1. 程式人生 > >第7周作業,最初完成結果

第7周作業,最初完成結果

作業題目
// 測試程式碼1:二維座標點類
Point pPoint = new Point(45,56);
Console.WriteLine(pPoint.X);// 應該輸出45
Console.WriteLine(pPoint.Y);//應該輸出56
Point pPoint2 = new Point();
Console.WriteLine(pPoint2.X);//輸出預設值10;
Console.WriteLine(pPoint2.Y);//輸出預設值20;

// 測試程式碼2:矩形類
Rectangle pRectangle = new Rectangle(5.5,4.0);
Console.WriteLine(pRectangle.L);// 輸出矩形的長 = 5.5;
Console.WriteLine(pRectangle.W);// 輸出矩形的寬 = 4.0;
Console.WriteLine(pRectangle.GetArea());// 輸出矩形的面積 22.0;
Console.WriteLine(pRectangle.GetPerimeter());// 輸出矩形的周長 19.0

// 測試程式碼3: 文字讀取類,用於讀取txt文字物件,需在D盤目錄下建立一個名稱為test的文字文件。
// 文字內容為 hello CSharp.文字讀取的相關方法需百度。
TxtReader pReader = new TxtReader(“d://test.txt”);//建構函式傳入的是檔案的路徑
string pContent = pReader.ReadContent();// 通過讀取內容的函式能夠讀取其所有內容
Console.WriteLine(pContent); // 輸出的內容應該為hello CSharp

// 測試程式碼4:文字建立類,用於建立txt文字物件。相關內容自行百度學習。
string pFileSavePath = “D://txtWriter.txt”;
TxtWriter pWriter = new TxtWriter(pFileSavePath);// 建構函式傳入需要建立的txt檔案的路徑
pWriter.WriteContent(“Hello CSharp”); // WriteContent方法寫入文字
TxtReader pReader = new TxtReader(pFileSavePath);
string pWriteContent = pReader.ReadContent();
Console.WriteLine(pWriteContent);// 輸出的內容應該為Hello CSharp;

// 測試程式碼5:多邊形類,繼承與多型練習,多型與繼承的概念可以自行百度
Polygon pPolygon1 = new RectangleClass(4,5);// 建構函式傳入長和寬
Polygon pPolygon2 = new SquareClass(5); // 建構函式傳入邊長
Console.WriteLine(pPolygon1.GetArea()); // 應該輸出20
Console.WriteLine(pPolygon2.GetArea()); // 應該輸出25

程式展示
class Point

{
    private int x;
    public int X
    {
        get
        {
            return x;
        }
        set
        {
            x = value;
        }
    }
    private int y;
    public int Y
    {
        get
        {
            return y;
        }

        set
        {
            y = value;
        }

    }
    public Point(int x = 10, int y = 20)
    {
        this.x = x;
        this.y = y;
    }
}

class Rectangle

{
    private double l;
    public double L
    {
        get
        {
            return l;
        }
        set
        {
            l = value;
        }
    }
    private double   w;
    public double   W
    {
        get
        {
            return w;
        }
        set
        {
            w = value;
        }
    }
    
    public double GetArea()
    {
        double   Area = this.l * this.w;
        return Area;
    }
    public double GetPerimeter()
    {
        double  Perimeter = 2*(this.l + this.w);
        return Perimeter;
    }
    public Rectangle(double   l, double   w)
    {
        this.l = l;
        this.w = w;
    }
}

class TxtReader

{
    private string path;
    public TxtReader(string s)
    {
        path  = s;
    }
    public string   ReadContent()
    {
        string Content = System.IO.File.ReadAllText(path );
        return Content;
    }
}

class TxtWriter

{
    private string path;
    public TxtWriter(string s)
    {
        path = s;
    }
    public void  WriteContent(string s)
    {
        string str1 = s;
       System.IO.File.WriteAllText(path , str1); 
    }
}

class Rectangle

{
    private double l;
    public double L
    {
        get
        {
            return l;
        }
        set
        {
            l = value;
        }
    }
    private double   w;
    public double   W
    {
        get
        {
            return w;
        }
        set
        {
            w = value;
        }
    }
    
    public double GetArea()
    {
        double   Area = this.l * this.w;
        return Area;
    }
    public double GetPerimeter()
    {
        double  Perimeter = 2*(this.l + this.w);
        return Perimeter;
    }
    public Rectangle(double   l, double   w)
    {
        this.l = l;
        this.w = w;
    }
}