具有不同基类的派生属性的 C# 继承类

时间:2021-03-31 19:06:08

标签: c# inheritance polymorphism multiple-inheritance

如果你觉得标题不好,请见谅。我的问题是我有一个带有属性 A 的基类 x。我有一个带有属性 A' 的派生类 x'。当我实现它时,它看起来更像下面。

public class Vehicle
{
    public List<Part> Parts {get; set;}
    public string Name {get; set;}

    public void GetParts()
    {
        this.Parts.Foreach(x => Console.WriteLine(x.Name));
    }
}

public class Part
{
    public string Name {get; set;}
    public string Place {get; set;}
}

public class CarPart : Part
{
    public string Setting {get; set;}
}

public class Car : Vehicle
{
    public new List<CarPart> Parts {get; set;}
}

当我实现上述类时,在访问属性部分时,我只能访问对象标识符。 例如在下面的代码中:

var xuv300 = new Car();
xuv300.GetParts();

虽然我将部件添加到 xuv300,但没有打印任何内容。因为有两个不同的属性,而基类表示的 Parts 属性没有任何值。

请帮我解决这个问题。

0 个答案:

没有答案
相关问题