Shape
class Shape
{
protected double L1, L2;
protected double density;
protected double pi = 3.1415926;
protected static int amount = 0;
public static int Amount
{
get { return amount; }
}
public Shape(double L1, double density)
{
this.L1 = L1;
this.density = density;
amount++;
}
public Shape(double L1, double L2, double density)
{
this.L1 = L1;
this.L2 = L2;
this.density = density;
amount++;
}
public virtual double CalVolume() { return 0; }
public virtual string GetName() { return ""; }
public double CalQuality() { return CalVolume() * density; }
public string Show()
{
string str="";
str += GetName()+"\t";
str += L1 + "\t" + L2 + "\t";
str += density + "\t";
str += CalVolume() + "\t";
str += CalQuality() + "\n";
return str;
}
}
Ball
class Ball : Shape
{
public Ball(double L1, double density) : base(L1, density) { }
public static Ball Create(double L1, double density)
{
Ball ball = null;
if (L1 <= 0 )
{
return ball;
}
else
{
ball = new Ball(L1, density);
return ball;
}
}
public override double CalVolume()
{
return pi * L1 * L1 * L1 * 4 / 3;
}
public override string GetName()
{
return "Ball";
}
}
Cube
class Cube : Shape
{
public Cube(double L1, double density) : base(L1, density) { }
public static Cube Create(double L1, double density)
{
Cube cube = null;
if (L1 <= 0 )
{
return cube;
}
else
{
cube = new Cube(L1, density);
return cube;
}
}
public override double CalVolume()
{
return L1 * L1 * L1 ;
}
public override string GetName()
{
return "Cube";
}
}
Cylinder
class Cylinder : Shape
{
public Cylinder(double L1, double L2, double density) : base(L1, L2, density) { }
public static Cylinder Create(double L1, double L2, double density)
{
Cylinder cylinder = null;
if (L1 <= 0 || L2 <= 0)
{
return cylinder;
}
else
{
cylinder = new Cylinder(L1, L2, density);
return cylinder;
}
}
public override double CalVolume()
{
return pi * L1 * L1 * L2;
}
public override string GetName()
{
return "Cylinder";
}
}
Pyramid
class Pyramid : Shape
{
public Pyramid(double L1, double L2, double density) : base(L1, L2, density) { }
public static Pyramid Create(double L1, double L2, double density)
{
Pyramid pyramid = null;
if (L1 <= 0 || L2 <= 0)
{
return pyramid;
}
else
{
pyramid = new Pyramid(L1, L2, density);
return pyramid;
}
}
public override double CalVolume()
{
return (L1 * L1 * L2) / 3;
}
public override string GetName()
{
return "Pyramid";
}
}
有做保護是很好,只是有一些狀況沒有處理乾淨。
回覆刪除針對form1.cs的75與85行,case 2與3的部分
if (txt_1.Text == "")
MessageBox.Show("Error");
建議修改成
if ((txt_1.Text == "")||(txt_2.Text == ""))
MessageBox.Show("Error");
不然程式會當掉的
感謝提醒
刪除第二組
回覆刪除int amount = Shape.Amount; 這部分有點多餘,且可能產生錯誤
建議把L1,L2的成員移至子類別,因為可能有些圖形不需要這麼多參數。
且遇到3個長度以上的形狀,又要修改父類別的成員個數,加上其他程式觀看者不容易知道方程式的意義(命名問題)
是第八組啦 打錯了
刪除(第九組)
回覆刪除1.類別Shape裡的Show()應該要寫在Form1.cs裡。
2.建議可以使用靜態陣列來減少switch case的使用,而且Form1.cs裡的程式太多重複,像是判斷物件是否隱藏(true or false)那邊。