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();
}
基类
class BMI
{
//memberVariables
private string newName;
private int newHeight;
private double newWeight;
//default constructor
public BMI(){}
//overloaded constructor
public BMI(string name, int height, double weight)
{
newName = name;
newHeight = height;
newWeight = weight;
}
//poperties
public string Get_Name
{
get { return newName; }
set { newName = value;}
}
public int Get_height
{
get { return newHeight; }
set { newHeight = value; }
}
public double Get_weight
{
get { return newWeight; }
set { newWeight = value; }
}
}
派生类
class Health : BMI
{
private int newSize;
public Health(int Size):base()
{
newSize = Size;
}
}
如何传递在基座参数从BMI基类中的重载构造函数转换为派生类? 任何时候我尝试将它们传递到基本参数,我得到无效的表达式错误。 或者我只需要将它们传递给主对象中的Health对象? 如
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;
}
}
我会建议将您的属性重命名为“高度”,“名称”和“重量”。让它们以'Get'作为前缀使得它看起来像是在调用一个方法或访问一个只读属性。 –