![]() |
#2
lxsxd2014-08-20 22:40
大家好,有哥们百度了这道题目的答案,在此,不再麻烦大家了。
代码如下: 源代码: ClsPoint.cs using System; using System.Collections.Generic; using System.Text; namespace __12 { //***************************基类clsPoint***************************** public class clsPoint { public double x; public double y; public clsPoint() { this.x = 0; this.y = 0; } public void setPoint(double _x,double _y) { this.x = _x; this.y = _y; } public void display() { Console.Write(" ({0},{1})", this.x, this.y); } } //*********************派生类clsLine********************************* public class clsLine : clsPoint { public clsPoint Point_start; public clsPoint Point_end; public clsLine(clsPoint Point_start, clsPoint Point_end) { this.Point_start=Point_start; this.Point_end = Point_end; } public double longness() { double l; l = System.Math.Sqrt((this.Point_start.y - this.Point_end.y) * (this.Point_start.y - this.Point_end.y) + (this.Point_start.x - this.Point_end.x) * (this.Point_start.x - this.Point_end.x)); return l; } } //*********************派生类clsRect********************************* public class clsRect : clsPoint { public clsPoint point1; public clsPoint point2=new clsPoint(); public clsPoint point3; public clsPoint point4=new clsPoint(); double l1,l2; public clsRect(clsPoint point1,clsPoint point2) { this.point1 = point1; this.point2.setPoint(point1.x, point2.y); this.point3 = point2; this.point4.setPoint(point2.x, point1.y); this.l1=point2.x-point1.x; this.l2=point2.y-point1.y; } public double girth() //周长 { return ((this.l1 + this.l2) * 2); } public double area() //面积 { return (this.l1 * this.l2); } } } Program.cs using System; using System.Collections.Generic; using System.Text; namespace __12 { class Program { static void Main(string[] args) { clsPoint point1 = new clsPoint(); clsPoint point2 = new clsPoint(); point1.setPoint(0,0); point2.setPoint(10,10); clsLine line1 = new clsLine(point1,point2); clsRect rect1 = new clsRect(point1,point2); Console.Write("由"); line1.Point_start.display(); line1.Point_end.display(); Console.WriteLine("构成的线段的长度为:{0:f}", line1.longness()); Console.Write("由"); rect1.point1.display(); rect1.point2.display(); rect1.point3.display(); rect1.point4.display(); Console.WriteLine("构成的矩形周长为:{0:f},面积为:{1:f}", rect1.girth(),rect1.area()); } } } |
一、题目
【5-12】把定义平面直角坐标系上的一个点的类clsPoint作为基类,派生出描述一个直线的类clsLine,再派生出一个矩形类clsRect类。要求方法能求出两点间的距离、矩形的周长和面积等。设计一个测试程序,并构造出完整的程序。
二、我的努力
namespace 书本练习题
{
class clsPoint
{
/// <summary>
/// 定义属性
/// </summary>
int x;
public int X
{
get { return x; }
set
{
if (value >= 60)
{
x = value;
}
else
{
x = 60;
}
}
}
int y;
public int Y
{
get { return y; }
set
{
if (value >= 75)
{
y = value;
}
else
{
y = 75;
}
}
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="xx"></param>
/// <param name="yy"></param>
public clsPoint(int xx, int yy)
{
this.x = xx;
this.y = yy;
}
public clsPoint()
{
this.x = 60;
this.y = 75;
}
/// <summary>
/// 定义方法
/// </summary>
public void display()
{
Console.WriteLine("x,y的坐标值为({0},{1})", x, y);
}
}
class Program
{
static void Main(string[] args)
{
clsPoint Dian = new clsPoint();
Dian.display();
clsPoint DianTwo = new clsPoint(78, 79);
DianTwo.display();
Console.ReadKey();
}
}
}
三、我的困难
1、我不知道如何派生出 clsLine类 和 clsRect类;
2、要有方法能计算两点之间的距离;要有方法能计算矩形的周长和面积;
此题困扰了我整整三天了,希望大侠高手们拔刀相助,小虾在此不胜感谢!