第七周学习内容

实现一下测试代码功能

      //测试代码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

测试1:

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;
    }
}

测试2

class Rectangle
{
    private double l;
    public double L
    {
        get
        {
            return l;
        }
        set
        {
            l = value;
        }
    }
    private double   w;
    public double    W
    {
        get
        {
         return double.Parse(w.ToString("0.0"));
        //return w;
        }
        set
        {
            w = value;
        }
    }
    public double    GetArea()
    {
        //double   Area = this.l * this.w;
        //string s1 = string.Format("{0:F1}", Area);
        //return s1;
        return l * w;
    }
    public string GetPerimeter()
    {
        double    Perimeter = 2*(this.l + this.w);
        string s2 = string.Format("{0:F1}", Perimeter);
        return s2;
        //return  Perimeter;
    }
    public Rectangle(double l, double w)
    {
        this.l = l;
        this.w = w;
    }
}

测试3:

 class TxtReader
{
    private string path;
    public string Path
    {
        get { return path; }
        set { path = value; }
    }
    public TxtReader(string path)
    {
        this.path  = path ;
    }
    public string   ReadContent()
    {
        string Content = System.IO.File.ReadAllText(path );
        return Content;
    }
}

测试4:

class TxtWriter
{
    private string path;
    public string Path
    {
        get { return path; }
        set { path = value; }
    }
    public TxtWriter(string path)
    {
        this.path = path;
    }
    public void  WriteContent(string s)
    {
        string str1 = s;
       System.IO.File.WriteAllText(path , str1); 
    }
}

测试5:

 abstract class Polygon
{
       public abstract int GetArea();
 }

class RectangleClass : Polygon
{
  private int length;
    public int Length
    {
        get { return length; }
        set { length = value; }
    }
    private int width;
    public int Width
    {
        get { return width; }
        set { width = value; }
    }

    public override int GetArea()
    {
        return this.Length * this.Width; ;
    }
    public RectangleClass(int length, int width)
    {
        this.Length = length;
        this.Width = width;
    }
}

class SquareClass : Polygon
{
    private int side;
    public int Side
    {
        get { return side; }
        set { side = value; }
    }

    public override int GetArea()
    {
        return this.Side * this.Side;
    }
    public SquareClass(int side)
    {
        this.Side = side;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43393323/article/details/84633943