关于c#:如何将重载构造函数中的基本参数传递给派生类

How to pass base parameters from an overloaded constructor into the derived class

本问题已经有最佳答案,请猛点这里访问。

1
2
3
4
5
6
7
8
9
10
11
static void Main(string[] args)
    {
        string name ="Me";
        int height = 130;
        double weight = 65.5;
        BMI patient1 = new BMI();
        BMI patient2 = new BMI(name,height,weight);

        Console.WriteLine(patient2.Get_height.ToString() + Environment.NewLine + patient1.Get_height.ToString() );
        Console.ReadLine();
    }

基础课

ZZU1

源级

1
2
3
4
5
6
7
8
class Health : BMI
{
    private int newSize;
    public Health(int Size):base()
    {
        newSize = Size;
    }
}

我怎么从超载建筑物的基准参数上经过BMI基准组,进入到后期的基准参数上呢?How do I pass in the base paramers from the overloaded constructor in the BMI base class into the derived c我试着把它们输入到基准参数中的任何时候我都会使表达失效。还是我必须把他们带到一个健康的对象?EG

1
2
3
4
5
6
7
8
9
10
11
12
class Health : BMI
{
    private int newSize;

     public Health(int Size, string Name, int Height, double Weight)
    {
        newSize = Size;
        base.Get_Name = Name
        base.Get_weight = Weight;
        base.Get_height = Height;
    }
}


构造函数不是继承的,因此是的,您需要为基类创建一个新的构造函数,但是您可以使用适当的参数调用基构造函数:

1
2
3
4
5
 public Health(int size, string name, int height, double weight)
    : base(name, height, weight)
{
    newSize = size;
}


这样地:

1
2
3
4
5
6
7
8
9
10
class Health : BMI
{
    private int newSize;

    public Health(int Size, string Name, int Height, double Weight)
        : base(Name, Height, Weight)
    {
        newSize = Size;
    }
}


为什么不能像这样调用传递参数的基类构造函数

1
2
3
4
5
public Health(int Size, string Name, int Height, double Weight)  
    : base(Name, Height, Weight)
{
    newSize = Size;
}