日期:2014-05-17  浏览次数:20960 次

C#--第四周实验--任务4--定义一个描述坐标点的CPoint类,派生出直线类Cline,在派生出矩形类CRect,实现一些功能。
/* (程序头部注释开始)
 * 程序的版权和版本声明部分
 * Copyright (c) 2011, 烟台大学计算机学院学生 
 * All rights reserved.
 * 文件名称:定义一个描述坐标点的CPoint类,派生出直线类Cline,在派生出矩形类CRect,求两点之间的距离以及矩形的面积与周长。
 * 作 者: 雷恒鑫 
 * 完成日期: 2012 年 09 月 22 日
 * 版 本 号: V1.0 
 * 对任务及求解方法的描述部分
 * 输入描述:
 * 问题描述:
 * 程序输出:
 * 程序头部的注释结束
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Four__week
{
    class Program
    {
        static void Main(string[] args)
        {
            CPoint c1 = new CPoint();
            CPoint c2 = new CPoint();
            CPoint c3 = new CPoint();
            c1.setpoint(1, 1);
            c2.setpoint(4, 5);
            c3.setpoint(4, 1);
            Cline m = new Cline();
            Console.WriteLine("两点之间的距离为:{0}", m.distence(c1, c2));
            Cline m1 = new Cline();
            m.set_length(c3, c2);
            m1.set_length(c1, c3);
            CRect CR = new CRect();
            Console.WriteLine("矩形的周长为:{0}", CR.perimeter(m, m1));
            Console.WriteLine("矩形的面积为:{0}", CR.area(m, m1));
            Console.ReadKey(true);
        }
    }

    class CPoint
    {
        private int x;
        private int y;

        public CPoint(int x1,int y2)
        {
            x = x1;
            y = y2;
        }

        public CPoint()
        {
            x = 60;
            y = 75;

        }

        public void display()
        {
            Console.WriteLine("x = {0}   y = {1}",x,y);
        }
        public void setpoint(int x1, int y1)
        {
            x = x1;
            y = y1;
        }
        public int get_x()
        {
            return x;
        }
        public int get_y()
        {
            return y;
        }
    }

    class Cline:CPoint
    {
        public double a;
        public Cline(int a1)
        {
            a = a1;
        }
        public Cline()
        {
            a = 0;
        }
        public double distence(CPoint c1,CPoint c2)
        {
            return a = Math.Sqrt((Math.Pow((c1.get_x()-c2.get_x()),2)+Math.Pow((c1.get_y()-c2.get_y()),2)));
        }
        public double get_length()
        {
            return a;
        }
        public void set_length(CPoint c1, CPoint c2)
        {
            a = distence(c1,c2);
        }
    }
    class CRect:Cline
    {
        public double m;
        public double n;
        public CRect(double a1,double b1)
        {
            m = a1;
            n = b1;
        }
        public CRect()
        {
            m = 0;
            n = 0;
        }
        public double perimeter(Cline c1,Cline c2)
        {
            return m = 2 * (c1.get_length() + c2.get_length());
        }

        public double area(Cline c1, Cline c2)
        {
            return n = c1.get_length() * c2.get_length();
        }
    }
}


 

运行结果:

 

经验积累

1.上面的程序还有一点点没有完善,主要是派生类构造函数那一块,因为涉及了Base ,说以学了Base 和This之间的区别之后再继续完善。

2.感觉和C++大同小异。