简体   繁体   English

继承的类可以访问基类吗

[英]does inherited class can access base class

If I have ThumbPhoto entity which inherits Photo entity and Photo entity inherits Entity<int> is it enough to use 如果我有ThumbPhoto实体继承了Photo实体,而Photo实体继承了Entity<int>足以使用

public class ThumbnailPhoto : Photo

or should I use 还是我应该使用

public class ThumbnailPhoto : Photo, Entity<int>

C# doesn't support multiple inheritance, so no. C#不支持多重继承,所以不可以。

That aside though, this: 尽管如此,这:

public class ThumbnailPhoto : Photo

Means that you'll have access to the Entity methods/properties because of 意味着由于以下原因,您将有权访问Entity方法/属性

public class Photo : Entity<int>

This principle works however deep your inheritance gets. 无论您继承的继承多深,此原则都有效。

Multiple inheritance is not allowed in C#. C#中不允许多重继承。 You cannot inherit from more than one class. 您不能从多个类中继承。 However hierarchical inheritance is fine. 但是,层次继承是可以的。 So it is ok to write: 因此可以这样写:

public class ThumbnailPhoto : Photo

and you also derive from Entity<int> 并且您还从Entity<int>派生

The first is sufficient. 第一个就足够了。

The base classes or interfaces from your base class are automatically inherited. 基类或基类的接口会自动继承。

In fact is Entity isn't an interface, the later isn't even valid, as you can only specify a single base class in C#. 实际上, Entity不是接口,后者甚至都不是有效的,因为您只能在C#中指定单个基类。

public class ThumbnailPhoto : Photo

Above is an option and sufficient, because nothing would be gained if you could also include Entity<int> assumping Photo is derived from Entity<int> 上面是一个选择并且是足够的,因为如果您还可以包括Entity<int> ,则将不会获得任何好处,因为假设Photo是从Entity<int>派生的

public class ThumbnailPhoto : Photo, Entity<int>

Above is example of multiple inheritance which is not supported in C# 上面是C# 支持的多重继承的示例


Do not confuse this with multiple interface implementation, which is supported: 不要将此与支持的多接口实现混淆:

public class ThumbnailPhoto : Photo, IEntity, IAmAnInterface

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

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