简体   繁体   English

Inheritance 最佳实践:属性也应该是接口吗?

[英]Best Practice of Inheritance: Property should be an interface too or not?

I have 2 interfaces and 2 classes:我有 2 个接口和 2 个类:

public interface interface A
{
     List<B> MyList { get; set; }
}

public interface interface B
{
    string Name { get; set; }
}

public class ImplementA : A
{
     List<ImplementB> MyList { get; set; }
}

public class ImplementB : B
{
    string Name { get; set; }
}

So this gives me an error because ImplementA doesn't fullfill the Interface A. It doesn't because I would have to have the property List of interface B instead of List of ImplementB.所以这给了我一个错误,因为 ImplementA 没有填充接口 A。它不是因为我必须拥有接口 B 的属性列表而不是 ImplementB 的列表。

What is the best practice to fullfill the Interface but still be able to have a concrete classe within classes AND a abstract interface in the interface?填充接口但仍然能够在类中有一个具体类和接口中有一个抽象接口的最佳实践是什么?

The example is very friendly to make it Generic .该示例非常友好,可以使其成为Generic

public interface A<T>
{
     List<T> MyList { get; set; }
}

public interface B
{
    string Name { get; set; }
}

public class ImplementA : A<ImplementB>
{
    public List<ImplementB> MyList { get; set; }
}

public class ImplementB : B
{
    public string Name { get; set; }
}

Or this one, upon your needs或者这个,根据您的需要

public class ImplementA : A<B>
{
    public List<B> MyList { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM