# QUIZ7 BTN ``` using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using _106_Quiz6; using _106_Quiz7; using System.Collections; namespace _106_Quiz6 { public partial class Form1 : Form { private Shape3D[] shapeArr = new Shape3D[100]; public Form1() { InitializeComponent(); } private void btnAddShape_Click(object sender, EventArgs e) { string shape = cboxShape.SelectedItem.ToString(); switch (shape) { case "球": shapeArr[Shape3D.Amount] = new Ball(Double.Parse(txtPara1.Text), Double.Parse(txtDensity.Text)); break; case "立方體": shapeArr[Shape3D.Amount] = new Cube(Double.Parse(txtPara1.Text), Double.Parse(txtDensity.Text)); break; case "圓柱體": shapeArr[Shape3D.Amount] = new Cylinder(Double.Parse(txtPara1.Text), Double.Parse(txtPara2.Text),Double.Parse(txtDensity.Text)); break; case "金字塔": shapeArr[Shape3D.Amount] = new Pyramid(Double.Parse(txtPara1.Text), Double.Parse(txtPara2.Text),Double.Parse(txtDensity.Text)); break; default: break; } txtAmountOfShape.Text = Shape3D.Amount.ToString(); txtBallAmount.Text = Ball.Amount.ToString(); txtCubeAmount.Text = Cube.Amount.ToString(); txtCylinderAmount.Text = Cylinder.Amount.ToString(); txtPyramidAmount.Text = Pyramid.Amount.ToString(); ShowAllShapeInfo(); } private void ShowAllShapeInfo() { string info=""; for (int i=0; i<Shape3D.Amount; i++) { info += (shapeArr[i].ShapeProperty() + "\r\n"); } txtMessage.Text = info; } private void cboxShape_SelectedIndexChanged(object sender, EventArgs e) { string shape = cboxShape.SelectedItem.ToString(); switch (shape) { case "球": lblPara1.Text = "半徑"; lblPara2.Visible= false; txtPara2.Visible = false; break; case "立方體": lblPara1.Text = "邊長"; lblPara2.Visible = false; txtPara2.Visible = false; break; case "圓柱體": lblPara1.Text = "半徑"; lblPara2.Visible = true; lblPara2.Text = "高"; txtPara2.Visible = true; break; case "金字塔": lblPara1.Text = "邊長"; lblPara2.Visible = true; lblPara2.Text = "高"; txtPara2.Visible = true; break; default: break; } } private void cBoxMaterial_SelectedIndexChanged(object sender, EventArgs e) { string material = cBoxMaterial.SelectedItem.ToString(); double density; bool canModified = false; switch (material) { case "鋁": density = 2.7; break; case "鐵": density = 7.87; break; case "鉛": density = 11.3; break; default: density = 0; canModified = true; break; } txtDensity.Text = density.ToString(); if (!canModified) txtDensity.Enabled = false; } private void btn_FindHeaviestShape_Click(object sender, EventArgs e) { string info = ""; double maxWeight = 0; int index = 0; while (shapeArr[index]!=null) { if(shapeArr[index].Weight() > maxWeight) { maxWeight = shapeArr[index].Weight(); info = shapeArr[index].ShapeProperty(); } index++; } txt_ShowHeaviestShape.Text = info; } } } ``` CY ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using _106_Quiz7; namespace _106_Quiz6 { class Cylinder:Shape3D { new private static int _amount; //隱藏繼承 private double _radius; private double _height; private static double _pi = 3.1415926; public Cylinder():base() { _radius = 0; _height = 0; _amount++; } public Cylinder(double radius, double height,double density):base(density) { Radius = radius; Height = height; _amount++; } new public static int Amount //隱藏繼承 { get { return _amount; } } public double Radius { get { return _radius; } set { if (value < 0) _radius = 0; else _radius = value; } } public double Height { get { return _height; } set { if (value < 0) _height = 0; else _height = value; } } public override double Volume() //改寫父類別的方法, 給其他類別使用故宣告為 public { return _pi * _radius * _radius * _height; } public override string ShapeProperty() //改寫父類別的方法, 給其他類別使用故宣告為 public { string str = string.Format("{0,8}", "Cylinder"); str += '\t'; str += string.Format("{0,8:F2}", _radius); str += '\t'; str += string.Format("{0,8:F2}", _height); str += '\t'; str += Property(); //重構, 降低重複的程式碼 return str; } } } ``` Shape3D ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _106_Quiz7 { class Shape3D { protected static int _amount; //被繼承的欄位 (資料成員)需宣告為 protected protected double _density; //被繼承的欄位 (資料成員)需宣告為 protected public Shape3D() { _density = 0; _amount++; } public Shape3D(double density) { Density = density; _amount++; } public double Density { get { return _density; } set { if (value < 0) _density = 0; else _density = value; } } public static int Amount //要給其他類別取用, 故宣告為 public { get { return _amount; } } public virtual double Volume() //虛擬函式, 子類別需進行 override { return 0; } public double Weight() //開放給其他類別使用, 故宣告成 public { return _density * Volume(); } public virtual string ShapeProperty() //虛擬函式, 子類別需進行 override { return ""; } protected string Property() //只讓子類別呼叫, 故宣告 protected, 抽出子類別重複的程式碼進行重構 { string str=""; str += string.Format("{0,8:F2}", _density); str += '\t'; str += string.Format("{0,8:F2}", Volume()); str += '\t'; str += string.Format("{0,8:F2}", Weight()); return str; } } } ```