您的位置:首页 > 编程语言 > C#

C#上机 第七周 任务1 继承

2012-10-14 19:58 399 查看
/*
* 程序头部注释开始
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:继承
* 作    者:薛广晨
* 完成日期:2011  年 10 月  14  日
* 版 本号:x1.0

* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述: 把定义平面直角坐标系上的一个点的类CPoint作为基类,
派生出描述一条直线的类Cline,再派生出一个矩形类CRect。
要求成员函数能求出两点间的距离、矩形的周长和面积等。
设计一个测试程序,并构造完整的程序。
* 程序输出:
* 程序头部的注释结束
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace xue
{
class Program
{
static void Main(string[] args)
{
CPoint cp1 = new CPoint(3,3);
CPoint cp2 = new CPoint();
CPoint cp3 = new CPoint();
cp1.display_CPoint();
cp2.display_CPoint();
cp3.display_CPoint();
cp3.set_x(3);
cp3.display_CPoint();

Cline cl1 = new Cline();
Console.WriteLine("两点之间的距离为:{0}", cl1.distance(cp1, cp3));
Cline cl2 = new Cline();
cl2.distance(cp2, cp3);

CRect cr = new CRect();
Console.WriteLine("矩形的周长为:{0}", cr.perimeter(cl1, cl2));
Console.WriteLine("矩形的面积为:{0}", cr.area(cl1, cl2));
Console.ReadKey(true);

}
}

class CPoint
{
private double x;
private double y;

public CPoint()
{
x = 0;
y = 0;
}

public CPoint(double x, double y)
{
this.x = x;
this.y = y;
}

public void set_x(double x)
{
this.x = x;
}

public void set_y(double y)
{
this.y = y;
}

public double get_x()
{
return this.x;
}

public double get_y()
{
return this.y;
}

public void display_CPoint()
{
Console.WriteLine("({0}, {1})", this.x, this.y);
}
}

class Cline : CPoint
{
private double length;

public double distance(CPoint c1, CPoint c2)
{
this.length = Math.Sqrt((c1.get_x() - c2.get_x()) * (c1.get_x() - c2.get_x()) + (c1.get_y() - c2.get_y()) * (c1.get_y() - c2.get_y()));
return this.length;
}

public double getLength()
{
return this.length;
}

public void set_length(CPoint c1, CPoint c2)
{
this.length = distance(c1, c2);
}

}

class CRect : Cline
{
public double perimeter(Cline c1, Cline c2)
{
double pm = c1.getLength() * 2 + c2.getLength() * 2;
return pm;
}

public double area(Cline c1, Cline c2)
{
return c1.getLength() * c2.getLength();
}
}
}

运行结果:

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  任务 c# distance class c string