《类的连续派生——C#第四周》

来源:互联网 发布:用java判断质数 编辑:程序博客网 时间:2024/06/05 16:35

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:    《类的连续派生——C#第四周》                         
* 作    者:       刘江波                      
* 完成日期:    2012     年   9    月    24    日
* 版 本 号:    v1.2     

* 对任务及求解方法的描述部分
* 问题描述:把定义平面直角坐标系上的一个点的类CPoint作为基类,派生出描述一条直线的类CLine,再派生出一个矩形类CRect。 
* 程序头部的注释结束
*/

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            CPoint p1 = new CPoint();            CPoint p2 = new CPoint();            p1.SetPoint(1, 2);            p2.SetPoint(4, 6);            p1.display_Point();            p2.display_Point();            CLine l1 = new CLine();            l1.GetLength(p1, p2);            l1.display_Line();            CRect r1 = new CRect();            r1.SetCrect();            r1.GetCircumference();            r1.GetArea();            r1.display_Rect();            Console.ReadKey();        }    }    class CPoint    {        public double x{get;set;}        public double y{get;set;}        public void SetPoint(double a,double b)        {            x = a;            y = b;        }        public void display_Point()        {            Console.WriteLine("坐标值:{0},{1}",Convert.ToString(x),Convert.ToString(y));        }    }    class CLine : CPoint    {        public double Length{get;set;}        public double GetLength(CPoint p1, CPoint p2)        {            x = p1.x - p2.x;            y = p1.y - p2.y;            Length = Math.Sqrt(x * x + y * y);            return Length;        }        public void display_Line()        {            Console.WriteLine("两点之间的距离为:{0}", Convert.ToString(Length));        }    }    class CRect : CLine    {        public double L{get;set;}        public double W{get;set;}        public void SetCrect()        {            Console.Write("请输入矩形的长:");            L = Convert.ToDouble(Console.ReadLine());            Console.Write("请输入矩形的宽:");            W = Convert.ToDouble(Console.ReadLine());        }        public double GetCircumference()        {            return 2 * (L + W);        }        public double GetArea()        {            return L * W;        }        public void display_Rect()        {            Console.WriteLine("周长为:{0}", Convert.ToString( 2 * (L + W)));            Console.WriteLine("面积为:{0}", Convert.ToString(L * W));        }     }}

原创粉丝点击