开发者

Is there a way to inherit properties from multiple classes in C#? [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Multiple Inheritance in C#

In the following example, I want the Shirt to automatically inherit the properties of both the Material and Pigment classes. Is this even possible in C#?

public class Material
{
    public enum FabricTy开发者_C百科pes { Cotton, Wool, Polyester, Nylon }
    public FabricTypes Fabric { get; set; }
    public Color Color { get; set; }
}

public class Pigment
{
    public enum PigmentQualities { Fine, Average, Poor }
    public PigmentQualities Quality { get; set; }
    public Color Color { get; set; }
}

public class Shirt : Material //, Pigment
{
    public Shirt()
    {
        Fabric = FabricTypes.Cotton;
        Color = new Color();
        //Quality = PigmentQualities.Fine;
    }
}

I'm having a hard time furnishing a better example, but this is essentially what I'm trying to do. I realize I can create interfaces, but those won't automatically inherit the properties. See, because I don't want to have to manually punch in all those properties every time I create a class that is similar to Shirt.


C# doesn't allow to inherit from multiple classes.

But why do you do it like this? Shirt could have 2 properties instead: Material and Pigment. Those could be set when you initialize a Shirt instance like passing it in a constructor with those properties set. Or create a constructor in which you can pass certain properties and instantiate Material and Pigment in that constructor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜