C#在別處寫出新的cs及調用該cs的方法

本來不太想寫,因為簡單,但說明起來很麻煩

目的:實現使用者可以輸入長寬,算回周長及面績、體績
步驟:
   一、在專案裡新增一個叫Shape的資料夾
   二、在該資料夾中新增2個檔案Rect.cs、Box.cs
   三、先寫Rect
   四、再寫Box繼承Rect
   五、如此一來,就可以在主程式中呼叫及調用





Rect.cs

class Rect
{
        
    private int length;
    private int width;
    //私有的長跟寬,誰都不能調用
        
    public Rect()
    {
        //建構子,給預設值
        length = 10;
        width = 8;
    }        
    public Rect(int length,int width)
    {
        //這個是Overload,與建構子同名,但有帶參數
        //這個參數會寫回private length 及 width
        this.length = length;
        this.width = width;
    }
        
    public int Area()
    {
        //當有其他人呼叫 Area(),回傳面績
        return length * width;
    }

    public int Perimeter()
    {
        //當有其他人呼叫 Perimeter(),回傳周長
        return (length + width) * 2;
    }

    public int Length
    {   
        //開一個方法讓別的人可以取得長
        get { return this.length; }
        //若要讓別人可以取得寬,要有個set 對應value
        //set { this.length = value; }
    }
    public int Width
    {
        get { return this.width; }
        //set { this.length = value; }
    }

    //Override Object類別中的 ToString()方法
    public String ToString()
    {
        String result = "";
        result += String.Format("長:{0}\r\n", this.length);
        result += String.Format("寬:{0}\r\n", this.width);
        result += String.Format("周長:{0}\r\n", this.Perimeter());
        result += String.Format("面積:{0}\r\n", this.Area());
        return result;
    }

}


Box.cs

class Box : Rect
{
    //Box 繼承 Rect
    private int height;
    public int Height
    {
        get { return this.height; }
    }

    public Box()
    {
        //建構子,預設的height為6
        this.height = 6;
    }
    public Box(int length, int width, int height) : base(length, width)
    {
        //overload,其中的length及width 繼承父親的 base(length, width)
        this.height = height;
    }

    public int Volume()
    {
        //求出體績回傳
        return this.Area() * this.height;
    }

    //Override Rect的Area()
    public int Area()
    {
        //總面績回傳
        return base.Area() * 2 + this.Length * this.height * 2 +
            this.Width * this.height * 2;
    }

    public new int Perimeter()
    {
        return base.Perimeter() * 2 + this.height * 4;
    }
    public String ToString()
    {
        String result = base.ToString();            
        result += String.Format("高:{0}\r\n", this.height);
        result += String.Format("體積:{0}\r\n", this.Volume());
        return result;
    }

}


主程式:

private void button3_Click(object sender, EventArgs e)
{
    String result = "";            
    Rect r1 = new Rect();
    //調用Rect的建構字
    result += r1.ToString() + "\r\n";

    Rect r2 = new Rect(50,30);
    //調用Rect的建構字,但給予長、寬
    result += r2.ToString() + "\r\n";         
            
    Shape.Box b1 = new Shape.Box();
    result += b1.ToString() + "\r\n";

    Shape.Box b2 = new Shape.Box(50, 30, 20);
    //調用的Shape.Box建構字,但給予長、寬、高
    result += b2.ToString() + "\r\n";


    textBox1.Text = result;
    //顯示所有結果
}

留言

熱門文章