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

C# 点 线 面

2016-04-08 18:52 441 查看
问题及代码:

把定义平面直角坐标系上的一个点的类CPoint作为基类,派生出描述一条直线的类Cline,再派生出一个矩形类CRect。要求成员函数能够求出两点间的距离、矩形的周长和面积等。

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

namespace ConsoleApplication1
{
class CPoint
{
public double x;
public double y;
public double GetX() { return x; }
public double GetY() { return y; }
public void setPoint(int m,int n){x=m;y=n;}

}
class Cline : CPoint
{
public double Dis(CPoint P1,CPoint P2)
{
double dis;
dis = Math.Sqrt((P1.GetX() - P2.GetX()) * (P1.GetX() - P2.GetX()) + (P1.GetY() - P2.GetY()) * (P1.GetY() - P2.GetY()));
return dis;
}
}
class CRect : Cline
{
public  CRect() { }
double length=0, area=0;
public double Getlength(CPoint p1,CPoint p4)
{
return length = 2 * (Math.Abs((p1.GetY() - p4.GetY())) + Math.Abs((p4.GetX() - p1.GetY())));
}
public double Getarea(CPoint p1,CPoint p4)
{
return area = Math.Abs(p1.GetY() - p4.GetY()) * Math.Abs(p4.GetX() - p1.GetY());
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("p1=(1,1),p4=(2,2)");
CPoint p1 = new CPoint();
CPoint p4 = new CPoint();
p1.setPoint(1,1);
p4.setPoint(2,2);
CRect cr=new CRect();

Cline cl1 = new Cline();

Console.WriteLine("p1p2={0}",cl1.Dis(p1,p4));
Console.WriteLine("矩形的周长为{0},面积为{1}", cr.Getlength(p1, p4), cr.Getarea(p1, p4));
Console.ReadKey();

}
}
}

运行结果:



知识点总结:

掌握集成类与父类之间成员的访问权限。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: