Form1.cs
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;
namespace WH6
{
public partial class Form1 : Form
{
public float density;
Shape[] obj = new Shape[10];
public Form1()
{
InitializeComponent();
}
private void btn_AddShape_Click(object sender, EventArgs e)
{
int amount = Shape.Amount;
if (Shape.Amount >= 10)
{
MessageBox.Show("最多只能輸入十個物體!");
return;
}
switch (cbo_ChooseMaterial.SelectedIndex)
{
case 0: //鉛
density = (float)11.35;
break;
case 1: //鋁
density = (float)2.70;
break;
case 2: //鐵
density = (float)7.87;
break;
}
switch (cbo_ChooseShape.SelectedIndex)
{
case 0: //球
obj[amount] = Shape.CreateBall(int.Parse(txt_Radius.Text));
obj[amount].density = density;
txt_ShowInformation.AppendText(obj[amount].Str + "\n");
break;
case 1: //正方體
obj[amount] = Shape.CreateCube(int.Parse(txt_Side.Text));
obj[amount].density = density;
txt_ShowInformation.AppendText(obj[amount].Str + "\n");
break;
case 2: //圓柱體
obj[amount] = Shape.CreateCylinder(int.Parse(txt_CylinderRadius.Text), int.Parse(txt_CylinderTall.Text));
obj[amount].density = density;
txt_ShowInformation.AppendText(obj[amount].Str + "\n");
break;
case 3: //金字塔
obj[amount] = Shape.CreatePyramid(int.Parse(txt_PyramidSide.Text), int.Parse(txt_PyramidTall.Text));
obj[amount].density = density;
txt_ShowInformation.AppendText(obj[amount].Str + "\n");
break;
}
txt_Amount.Text = Shape.Amount.ToString();
}
public void ShowGroupBox()
{
gro_BallNumber.Visible = false;
gro_CubeNumber.Visible = false;
gro_Cylinder.Visible = false;
gro_Pyramid.Visible = false;
switch (cbo_ChooseShape.SelectedIndex)
{
case 0: //球
gro_BallNumber.Visible = true;
break;
case 1: //正方體
gro_CubeNumber.Visible = true;
break;
case 2: //圓柱體
gro_Cylinder.Visible = true;
break;
case 3: //金字塔
gro_Pyramid.Visible = true;
break;
}
}
private void cbo_ChooseShape_SelectedIndexChanged(object sender, EventArgs e)
{
ShowGroupBox();
}
private void btn_Calculation_Click(object sender, EventArgs e)
{
int i ;
for (i = 0; i < Shape.Amount; i++)
{
double weight;
weight = obj[i].Volume * obj[i].Density;
txt_Calculate.AppendText(obj[i].Str);
txt_Calculate.AppendText("\t");
txt_Calculate.AppendText(Math.Round(obj[i].density, 2).ToString());
txt_Calculate.AppendText("\t");
txt_Calculate.AppendText(Math.Round(obj[i].Volume, 3).ToString());
txt_Calculate.AppendText("\t");
txt_Calculate.AppendText(Math.Round(weight, 3).ToString() + "\n");
}
}
}
}
Shape.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WH6
{
class Shape
{
protected static int amount = 0;
protected float volume;
protected float pi = (float)3.1415926;
protected string str = "";
public float density;
public Shape()
{
}
public Shape(double num1)
{
amount++;
}
public Shape(double num1, double num2)
{
amount++;
}
public static int Amount
{
get { return amount; }
}
public string Str
{
get { return str; }
}
public float Volume
{
get { return volume; }
}
public float Density
{
get { return density; }
}
public static Shape CreateBall(float num1)
{
Shape obj;
obj = new Ball(num1);
return obj;
}
public static Shape CreateCube(float num1)
{
Shape obj;
obj = new Cube(num1);
return obj;
}
public static Shape CreateCylinder(float num1, float num2)
{
Shape obj;
obj = new Cylinder(num1, num2);
return obj;
}
public static Shape CreatePyramid(float num1, float num2)
{
Shape obj;
obj = new Pyramid(num1, num2);
return obj;
}
}
}
Ball.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WH6
{
class Ball : Shape
{
public Ball(float num1)
: base(num1)
{
volume = (4* pi * num1* num1 * num1)/3 ;
str += "Ball" + "\t" + num1.ToString() + "\t";
}
}
}
Cube.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WH6
{
class Cube : Shape
{
public Cube(float num1)
: base(num1)
{
volume = num1 * num1 * num1;
str = "Cube" + "\t" + num1.ToString() + "\t";
}
}
}
Cylinder.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WH6
{
class Cylinder : Shape
{
public Cylinder(float num1, float num2)
: base(num1, num2)
{
volume = pi * num1 * num1 * num2 ;
str = "Cylinder" + "\t" + num1.ToString() + "\t" + num2.ToString();
}
}
}
Pyramid.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WH6
{
class Pyramid : Shape
{
public Pyramid(float num1, float num2)
: base(num1, num2)
{
volume = (num1 * num1 * num2)/3 ;
str = "Pyramid" + "\t" + num1.ToString() + "\t" + num2.ToString();
}
}
}
執行檔
把 Create 子類別的物件的工作放在父類別並不是一個好方法
回覆刪除了解
刪除還是覺得float跟double混用不太好,要嘛全部float,不然就是用double
回覆刪除噢噢因為我之前是都用double
刪除後來發現不需要,所以想說通通改用float
weight沒改到,謝謝提醒
==第七組==
回覆刪除應該要在Shape裡加入density的屬性,Shape()-->此建構子未用到建議可以刪除。
作者已經移除這則留言。
刪除Shape裡面有destiny屬性
刪除Shape()原為以備不時之需,之後會改善,謝謝提醒
第八組
回覆刪除感覺有些東西可以分開寫在各個子類別內,像是有些只有一個參數有些有兩個,建議可以讓子類別去產生各自的成員,且在計算上都用num計算容易讓別的使用者看不懂..
各形狀的Creat code 可以考慮寫在各子類別的建構子中。
因為在打這份code的時候還沒看最新的教學影片
刪除所以我想說
放在父類別的參數因為每個子類別都有使用到
若每個子類別都各別去生成會浪費空間
命名和create的部分之後會改善謝謝
林高遠評論:
回覆刪除缺點:
1. 密度那邊強制轉型float沒有必要, 因為src和dst都是float
2. Shape的數量限制不能留到runtime決定,太沒有彈性
3. density assign 當作你的CreateBall或建構子的參數就好,用單獨一行程式set多此一舉
4. ShowGroupBox() 的部分,一律先把所有物件Visible = false,再依選擇 Visible = true, 這樣程式碼的可移植性會很不好。因為,當你這些要做顯示控制的物件數目有更動,程式碼就要一次改好幾個地方,沒辦法整塊改
5. for loop 的 index 不用在scope 外宣告,那是C89的寫法
6. 把weight的計算寫在介面很不好;weight的計算應該寫成一個抽象或虛擬方法,然後宣告在基礎類別
7. 有一大堆Create方法,建構子卻空空如也
缺點 4. 的部分,您可以去看我的程式碼,我寫的就是可以整塊改的模式
刪除gro_BallNumber以及其他類似的gro_XXX 是指你們把輸入參數用的txt用groupbox包起來,再以combobox決定要不要顯示groupbox嗎?
回覆刪除感覺是不錯的方式,可以比較容易決定參數要不要顯示