執行檔 DensityofMatel.cs class DensityofMatel
{
//Parameters
protected double density=0;
protected static string[] matelName = { "鋁", "金", "銅" };
protected static double[] matelDensity = { 2.7, 19.3, 8.96 };
//Method
public DensityofMatel(string matel)
{
int j = 0;
for (int i = 0; i < matelName.Length; i++)
{
if (matel == matelName[i])
j = i;
}
density = matelDensity[j];
}
}
Shape3D.cs class Shape3D:DensityofMatel
{
//-------------Parameters-------------------
protected static double pi = 3.1415926;
protected static int amount = 0;
//--------------Attribute-----------------
public string ShapeName()
{
return getShapeName();
}
public static int Amount
{
get { return amount; }
}
public static double Pi
{
get { return pi; }
}
//-------------Constructor-----------------------
public Shape3D(string matel)
: base(matel)
{
amount++;
}
//Method
protected virtual string getShapeName()
{
return "";
}
public virtual double Volume()
{
return 0;
}
public double Mass()
{
return Volume() * density;
}
}
Ball.cs class Ball : Shape3D
{
//--------------Parameters--------------
new private static int amount = 0;
private double radius;
//--------------Attribute--------------
public double Radius
{
get { return radius; }
}
new public static int Amount
{
get { return amount; }
}
//--------------contructor--------------
public Ball(string matel, double radius)
: base(matel)//建構子
{
this.radius = radius;
amount++;
}
//--------------Method--------------
public static Ball Create(string matel, double radius)
{
Ball ball = null;
if (matel == null || radius <= 0)
return ball;
else
{
ball = new Ball(matel, radius);
return ball;
}
}
protected override string getShapeName()
{
return "Ball";
}
public override double Volume()
{
return pi * radius * radius * radius * 4 / 3;
}
}
}
Cylinder.cs class Cylinder : Shape3D
{
//--------------Parameters--------------
new private static int amount = 0;
private double radius;
private double height;
//--------------Attribute---------------
public double Radius
{
get { return radius; }
}
public double Height
{
get { return height; }
}
new public static int Amount
{
get { return amount; }
}
//--------------Contructor--------------
public Cylinder(string matel, double radius, double height)
: base(matel)
{
this.radius = radius;
this.height = height;
amount++;
}
//--------------Method--------------
public static Cylinder Create(string matel, double radius, double height)
{
Cylinder cylinder = null;
if (matel == null || radius <= 0 || height <= 0)
return cylinder;
else
{
cylinder = new Cylinder(matel, radius, height);
return cylinder;
}
}
protected override string getShapeName()
{
return "Cylinder";
}
public override double Volume()
{
return radius * radius * pi * height;
}
}
Square.cs class Square : Shape3D
{
//--------------Parameters--------------
new private static int amount = 0;
private double length = 0;
//--------------Attribute--------------
public double Length
{
get { return length; }
}
new public static int Amount
{
get { return amount; }
}
//--------------Contructor--------------
public Square(string matel, double length)
: base(matel)
{
this.length = length;
amount++;
}
//--------------Method--------------
public static Square Create(string matel, double length)
{
Square square = null;
if (matel == null || length <= 0)
return square;
else
{
square = new Square(matel, length);
return square;
}
}
protected override string getShapeName()
{
return "Square";
}
public override double Volume()
{
return length * length * length;
}
}
Pyramid.cs class Pyramid : Shape3D
{
//--------------Parameters--------------
new private static int amount = 0;
private double length = 0;
private double height = 0;
//--------------Attribute--------------
public double Length
{
get { return length; }
}
public double Height
{
get { return height; }
}
new public static int Amount
{
get { return amount; }
}
//--------------Contructor--------------
public Pyramid(string matel, double length, double height)
: base(matel)
{
this.length = length;
this.height = height;
amount++;
}
//--------------Method--------------
public static Pyramid Create(string matel, double length, double height)
{
Pyramid pyramid = null;
if (matel == null || length <= 0 || height <= 0)
return pyramid;
else
{
pyramid = new Pyramid(matel, length, height);
return pyramid;
}
}
protected override string getShapeName()
{
return "Pyramid";
}
public override double Volume()
{
return length * length * height / 3;
}
}
Form.cs public partial class Form1 : Form
{
Shape3D[] ArrShape = new Shape3D[100];
Shape3D Temp;//檢查用
public Form1()
{
InitializeComponent();
txt_Height.Visible = false;
lbl_Height.Visible = false;
txt_Length.Visible = false;
lbl_Length.Visible = false;
txt_Radius.Visible = false;
lbl_Radius.Visible = false;
}
private void cbox_ShapeSelect_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cbox_ShapeSelect.SelectedIndex)
{
case 0://Ball
txt_Height.Visible = false;
lbl_Height.Visible = false;
txt_Length.Visible = false;
lbl_Length.Visible = false;
txt_Radius.Visible = true;
lbl_Radius.Visible = true;
break;
case 1://Cylinder
txt_Height.Visible = true;
lbl_Height.Visible = true;
txt_Length.Visible = false;
lbl_Length.Visible = false;
txt_Radius.Visible = true;
lbl_Radius.Visible = true;
break;
case 2://Square
txt_Height.Visible = false;
lbl_Height.Visible = false;
txt_Length.Visible = true;
lbl_Length.Visible = true;
txt_Radius.Visible = false;
lbl_Radius.Visible = false;
break;
case 3://Pyramid
txt_Height.Visible = true;
lbl_Height.Visible = true;
txt_Length.Visible = true;
lbl_Length.Visible = true;
txt_Radius.Visible = false;
lbl_Radius.Visible = false;
break;
default:
break;
}
}
private void btn_Add_Click(object sender, EventArgs e)
{
switch (cbox_ShapeSelect.SelectedIndex)
{
case 0:
if (txt_Radius.Text == "")
{
MessageBox.Show("請輸入參數");
return;
}
Temp = Ball.Create(cbox_MaterialSelect.Text, double.Parse(txt_Radius.Text));
break;
case 1:
if (txt_Radius.Text == "" || txt_Height.Text == "")
{
MessageBox.Show("請輸入參數");
return;
}
Temp = Cylinder.Create(cbox_MaterialSelect.Text, double.Parse(txt_Radius.Text), double.Parse(txt_Height.Text));
break;
case 2:
if (txt_Length.Text == "")
{
MessageBox.Show("請輸入參數");
return;
}
Temp = Square.Create(cbox_MaterialSelect.Text, double.Parse(txt_Length.Text));
break;
case 3:
if (txt_Length.Text == "" || txt_Height.Text == "")
{
MessageBox.Show("請輸入參數");
return;
}
Temp = Pyramid.Create(cbox_MaterialSelect.Text, double.Parse(txt_Length.Text), double.Parse(txt_Height.Text));
break;
default:
break;
}
if (Temp == null)
{
MessageBox.Show("參數輸入錯誤");
return;
}
else
ArrShape[Shape3D.Amount - 1] = Temp;
txt_showamount.Text = ShowAmount();
}
private void btn_Caculus_Click(object sender, EventArgs e)
{
txt_ShowCaculusRsults.Text = CaculusResults();
}
private string ShowAmount()
{
string str = "";
str += "目前總數:" + Shape3D.Amount.ToString();
str += "\r\n";
str += "球型個數:" + Ball.Amount.ToString();
str += " ";
str += "圓柱體個數:" + Cylinder.Amount.ToString();
str += " ";
str += "正方體個數:" + Square.Amount.ToString();
str += " ";
str += "金字塔個數:" + Pyramid.Amount.ToString();
return str;
}
private string CaculusResults()
{
string str = "";
for (int i = 0; i < Shape3D.Amount; i++)
{
str += ArrShape[i].ShapeName();
str += "體積為:" + ArrShape[i].Volume();
str += " ";
str += "質量為:" + ArrShape[i].Mass();
str += "\r\n";
}
return str;
}
}
第八組:
回覆刪除用類別去紀錄材質與密度這個方法很酷
但感覺上只要再shpae3D裡面新增,"密度"跟"材質名稱"兩個成員,然後再建構的時候存入就好了 .. ?
我覺得這個程式的架構算是蠻清楚的,不過金字塔的體積是不是算錯了??
回覆刪除謝謝提醒 已修正
刪除因子類別僅能繼承一父類別,所以此處將 Shape3D.cs 繼承 DensityofMatel.cs,這樣的架構不是很正確!
回覆刪除參考方法:
若要將 DensityofMatel.cs 保留,應於 Form1.cs 宣告 metirial density 並傳入 Shape3D.cs 的 method 進行計算。