该题增加一个派生圆类clsPark,求以两点为半径的半径、周长和面积。
我照猫画虎,完成了这个派生类的编写:
namespace 书本练习题
{
public class clsPoint
{
//***************************基类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=Math.Abs ( point2.x-point1.x);
this.l2=Math.Abs ( point2.y-point1.y);
}
public double girth()
//周长
{
return ((this.l1 + this.l2) * 2);
}
public double area()
//面积
{
return (this.l1 * this.l2);
}
}
//*********************派生类clsPark*********************************
public class clsPark : clsPoint
{
public clsPoint PointOne;
public clsPoint PointTwo;
public clsPark(clsPoint _PointOen, clsPoint _PointTwo)
{
this.PointOne = _PointOen;
this.PointTwo = _PointTwo;
}
double dbradii;
public double radiiTwo()
// 半径
{
clsLine line1 = new clsLine(this .PointOne ,this .PointTwo );
double Radii=line1.longness ();
this.dbradii = Radii;
return Radii;
}
public double girthTwo()
//周长
{
double Cr = Math.PI * this.dbradii * 2;
return Cr;
}
public double areaTwo()
//面积
{
double Sr = Math.PI * this.dbradii * this.dbradii;
return Sr;
}
}
}
class Program
{
static void Main(string[] args)
{
//计算线段的长度
clsPoint point1 = new clsPoint();
clsPoint point2 = new clsPoint();
point1.setPoint(0, 0);
point2.setPoint(10, 10);
clsPoint .clsLine line1 = new clsPoint .clsLine(point1, point2); //此处把实例对象作为参数进行传递
Console.Write("由");
line1.Point_start.display();
line1.Point_end.display();
Console.WriteLine("构成的线段的长度为:{0:f}", line1.longness()); //线段的长度
//计算矩形周长和面积
clsPoint.clsRect rect1 = new clsPoint.clsRect(point1, point2); //此处把实例对象作为参数进行传递
Console.Write("由");
rect1.point1.display();
rect1.point2.display();
rect1.point3.display();
rect1.point4.display();
Console.WriteLine("构成的矩形周长为:{0:f},面积为:{1:f}", rect1 .girth (), rect1.area());
//计算圆的半径、周长和面积
clsPoint.clsPark park1= new clsPoint .clsPark (point1,point2);
Console.Write("由");
park1.PointOne.display();
park1.PointTwo.display();
Console.WriteLine("构成的圆的半径为:{0:f},周长为:{1:f},面积为:{2:f}", park1.radiiTwo (),park1 .girthTwo (),park1.areaTwo ());
Console.ReadKey();
}
}
}