C#编程基础 实验(4)

来源:互联网 发布:结婚照幻灯片制作软件 编辑:程序博客网 时间:2024/05/16 18:12


把定义平面直角坐标系上的一个点的类CPoint作为基类,派生出描述一条直线的类Cline,再派生出一个矩形类CRect。

代码如下:

using System;namespace Program0{    class CPoint    {        public double x { get; set; }        public double y { get; set; }        public CPoint() { }        public CPoint(double x, double y)        {            this.x = x;            this.y = y;        }    }     class CLine : CPoint    {    public CPoint EPoint{get;set;}    public double x2{get{return EPoint.x;}set{EPoint.x=value;}}    public double y2{get{return EPoint.y;}set{EPoint.y=value;}}         public CLine()    {        EPoint=new CPoint();    }         public CLine(CPoint p1,CPoint p2)    {        base.x=p1.x;        base.y=p1.y;        EPoint=p2;    }         public double LineLength{        get{            return Math.Sqrt((x2-x)*(x2-x)+(y2-y)*(y2-y));        }    }    }    class CRect :CLine    {               public double Perimeter        {            get            {                return 2 * (Math.Abs(x2 - x) + Math.Abs(y2 - y));            }        }        public double Area        {            get            {                return Math.Abs((x2 - x) * (y2 - y));            }        }    }    class Program    {               static void Main(string[] args)        {            CRect rect = new CRect() {x = 1, y = 2, x2 = 0, y2 = 1 };            Console.WriteLine(rect.LineLength);            Console.WriteLine(rect.Perimeter);            Console.WriteLine(rect.Area);                        Console.ReadKey();        }    }    }

1 0