1

If I have ThumbPhoto entity which inherits Photo entity and Photo entity inherits Entity<int> is it enough to use

public class ThumbnailPhoto : Photo

or should I use

public class ThumbnailPhoto : Photo, Entity<int>

4

4 回答 4

5

C# doesn't support multiple inheritance, so no.

That aside though, this:

public class ThumbnailPhoto : Photo

Means that you'll have access to the Entity methods/properties because of

public class Photo : Entity<int>

This principle works however deep your inheritance gets.

于 2013-10-02T20:25:26.397 回答
2

Multiple inheritance is not allowed in 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>

于 2013-10-02T20:27:01.143 回答
1

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#.

于 2013-10-02T20:25:21.960 回答
1
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>

public class ThumbnailPhoto : Photo, Entity<int>

Above is example of multiple inheritance which is not supported in C#


Do not confuse this with multiple interface implementation, which is supported:

public class ThumbnailPhoto : Photo, IEntity, IAmAnInterface
于 2013-10-02T20:26:28.580 回答