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'作爲前綴使得它看起來像是在調用一個方法或訪問一個只讀屬性。 –