第六周作業程式碼參考
執行檔
2014年4月14日 星期一
[Code Review] Team 02 - Hw06 Other 01
老師在之前的投影片有提到:父類別的參考可以用來記錄子類別的物件,但就無法使用子類別的特有屬性與方法了。
但有時候還是需要得到子類別特有的屬性或使用特有的方法時,這邊提供了一個寫法給大家參考(但可能很爛),也回應一些組別遇到的問題。這個方法建立在多型的概念上。
按我顯示程式碼
但有時候還是需要得到子類別特有的屬性或使用特有的方法時,這邊提供了一個寫法給大家參考(但可能很爛),也回應一些組別遇到的問題。這個方法建立在多型的概念上。
按我顯示程式碼
class Program
{
static void Main(string[] args)
{
Shape3D MyCube = new Cube(1.0);
Console.WriteLine(MyCube.GetFirstFeature().ToString());
Console.ReadKey();
// Output: 1
}
}
class Shape3D
{
/******** 方法 ********/
public virtual double GetFirstFeature()
{
return 0.0;
}
}
class Cube : Shape3D
{
/******** 屬性 ********/
double Side = 0.0;
/******** 建構子 ********/
public Cube(double p_side)
{
Side = p_side;
}
/******** 方法 ********/
public override double GetFirstFeature()
{
return Side;
}
}
2014年4月13日 星期日
[Code Review] Team 09 - Hw06
在此超有誠意的附上整個專案檔
https://drive.google.com/file/d/0B9vj2TjBHPutWVBablNsVU5NNTJ0MTdmd0lrQjRGQ0FlbU00/edit?usp=sharing
https://drive.google.com/file/d/0B9vj2TjBHPutWVBablNsVU5NNTJ0MTdmd0lrQjRGQ0FlbU00/edit?usp=sharing
[Code Review] Team 03 - Hw06
執行檔 Form1.cs 按我顯示
Shape
public Form1()
{
InitializeComponent();
InitialSet();
}
double[] density = { 2.7, 7.9, 11.34 };
Shape[] shape= new Shape[50];
private void cBox_shape_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cBox_shape.SelectedIndex)
{
case 0:
lbl_1.Text = "半徑";
txt_1.Visible = true;
lbl_1.Visible = true;
txt_2.Visible = false;
lbl_2.Visible = false;
break;
case 1:
txt_1.Visible = true;
lbl_1.Visible = true;
txt_2.Visible = false;
lbl_2.Visible = false;
lbl_1.Text = "邊長";
break;
case 2:
lbl_1.Text = "半徑";
txt_1.Visible = true;
lbl_1.Visible = true;
txt_2.Visible = true;
lbl_2.Visible = true;
break;
case 3:
lbl_1.Text = "邊長";
txt_1.Visible = true;
lbl_1.Visible = true;
txt_2.Visible = true;
lbl_2.Visible = true;
break;
default:
MessageBox.Show("Error");
break;
}
}
private void btn_add_Click(object sender, EventArgs e)
{
int amount = Shape.Amount;
switch (cBox_shape.SelectedIndex)
{
case 0:
if (txt_1.Text == "")
MessageBox.Show("Error");
else
{
Ball ball = Ball.Create(double.Parse(txt_1.Text), density[cBox_material.SelectedIndex]);
shape[amount] = ball;
textBox1.AppendText("Ball" + "\t" + txt_1.Text + "\t" + txt_2.Text + "\n");
}
break;
case 1:
if (txt_1.Text == "")
MessageBox.Show("Error");
else
{
Cube cube = Cube.Create(double.Parse(txt_1.Text), density[cBox_material.SelectedIndex]);
shape[amount] = cube;
textBox1.AppendText("Cube" + "\t" + txt_1.Text + "\t" + txt_2.Text + "\n");
}
break;
case 2:
if (txt_1.Text == "")
MessageBox.Show("Error");
else
{
Cylinder cylinder = Cylinder.Create(double.Parse(txt_1.Text), double.Parse(txt_2.Text), density[cBox_material.SelectedIndex]);
shape[amount] = cylinder;
textBox1.AppendText("Cylinder" + "\t" + txt_1.Text + "\t" + txt_2.Text+"\n");
}
break;
case 3:
if (txt_1.Text == "")
MessageBox.Show("Error");
else
{
Pyramid pyramid = Pyramid.Create(double.Parse(txt_1.Text), double.Parse(txt_2.Text), density[cBox_material.SelectedIndex]);
shape[amount] = pyramid;
textBox1.AppendText("Pyramid" + "\t" + txt_1.Text + "\t" + txt_2.Text + "\n");
}
break;
default:
MessageBox.Show("Error");
break;
}
txt_number.Text = Shape.Amount.ToString();
}
private void InitialSet()
{
txt_1.Visible = false;
lbl_1.Visible = false;
txt_2.Visible = false;
lbl_2.Visible = false;
}
private void btn_calculate_Click(object sender, EventArgs e)
{
for (int i = 0; i < Shape.Amount; i++)
{
textBox2.AppendText(shape[i].Show());
}
}
}
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";
}
}
訂閱:
意見 (Atom)
