为什么派生类不能使用扩展该接口的类型覆盖基类的接口类型abstract属性?

时间:2015-02-17 19:39:03

标签: c# inheritance interface

在下面的代码中,编译器抱怨B没有实现抽象类TestProperty的{​​{1}}。 A源自ITest2,因此它实现了ITest1所拥有的所有内容。为什么这不可能?

ITest1

2 个答案:

答案 0 :(得分:8)

这样做不安全,因为你可以这样做:

interface ITest3 : ITest1 { }
public class Test3 : ITest3 { }

A b = new B();
b.TestProperty = new Test3();

Test3 ITest2未按B的要求实施{{1}}。

答案 1 :(得分:2)

使A类通用

Public abstract class A<TTest>
    Where TTest : ITest1
 {
    Public abstract TTest TestProperty {get; set;}
 }

 Public class B : A<ITest2>
 {
 ....
 }
相关问题