通過類繼承計算梯形面積
阿新 • • 發佈:2018-12-06
實現效果:
知識運用:
c#中實現繼承的語法格式:Class DerivedClass:BaseClass{ }
繼承類時 ,必須在子類和基類之間使用冒號(:)
實現程式碼:
private void button1_Click(object sender, EventArgs e) { double argU, argD, argH; bool safe=double.TryParse(textBox1.Text,out argU)& double.TryParse(textBox2.Text,out argD)& double.TryParse(textBox3.Text,out argH); if (safe) { trapeziaArea tar = new trapeziaArea(); tar.SD = argU; tar.XD = argD; tar.HEIGHT = argH; label1.Text = ("面積: " + tar.area().ToString()); } else { MessageBox.Show("請輸入正確的數字"); } } class trapezia { private double sd; private double xd; private double height; public double SD { get { return sd; } set { if (value > 0)sd = value; } } public double XD { get { return xd; } set { if (value > 0)xd = value; } } public double HEIGHT { get { return height; } set { if (value > 0)height = value; } } } class trapeziaArea:trapezia { public double area() { return (SD + XD) * HEIGHT / 2; } }