HEJ家伙,C#继承 - >的类层次结构
我理解在C#中继承的过程中存在问题。我正在做功课,我想分享我想出的代码。我也包括这个任务。
任务:
Work 1:
Develop a hierarchic structure of classes: shape, circle and cylinder:
Write the base class Shape which has two fields x and y coordinates The function
Display() returns a text string, which contains the point position.
The derived classes Circle and Cylinder inherit x , y coordinates and the method
Display() from base class Shape. You define the new necessary fields and methods
and you override the method Display() to return a text string with the coordinates,
the area and/or the volume.
The computing formulas are:
Circle area : p * r 2
Cylinder area: (2*p * r 2) + (2*p * r * h)
Cylinder volume: p * r 2 * h
这是我的代码有:
类Shape:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_Work_1
{
class Shape
{
public int xCoordinate = 0;
public int yCoordinate = 2;
public virtual void Display()
{
Console.WriteLine("The position of the point is: [{0};{1}].", xCoordinate, yCoordinate);
}
}
}
类圈:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_Work_1
{
class Circle : Shape
{
public override void Display()
{
double PI = Math.PI;
double radius = 2;
double circleArea = (PI * radius * radius);
Console.WriteLine("The area of the circle is: {0:F2}", circleArea);
}
}
}
类气缸:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_Work_1
{
class Cylinder : Shape
{
public override void Display()
{
double PI = Math.PI;
double radius = 2;
double height = 5.5;
double cylinderArea = (2* PI * radius * radius) + (2 * PI * radius * height);
Console.WriteLine("The area of the cylinder is: {0:F2}", cylinderArea);
double cylinderVolume = (PI * radius * radius * height);
Console.WriteLine("The volume of the cylinder is: {0:F3}\n", cylinderVolume);
}
}
}
而且主类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_Work_1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("A: Work 1\n");
Shape position = new Shape();
position.Display();
Circle circleArea = new Circle();
circleArea.Display();
Cylinder cylinderArea = new Cylinder();
cylinderArea.Display();
}
}
}
我想知道我在哪里得到它错了。继承的想法对我来说有点难以理解。我如何改进这个练习来完成任务?
谢谢你的回答!五
编辑:
我已编辑的代码,所以现在应该有适当的inherting。最后一个问题。我应该在哪里放置代码Console.WriteLine来获取输出?
类形状:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_Work_1
{
class Shape
{
public int xCoordinate = 0;
public int yCoordinate = 2;
public string Display()
{
string xCoordinateString = xCoordinate.ToString();
string yCoordinateString = yCoordinate.ToString();
return xCoordinateString + yCoordinateString;
}
}
}
类圆圈:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_Work_1
{
class Circle : Shape
{
public new string Display()
{
double PI = Math.PI;
double radius = 2;
double circleArea = (PI * radius * radius);
string circleAreaString = circleArea.ToString();
return circleAreaString;
}
}
}
类汽缸:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_Work_1
{
class Cylinder : Circle
{
public string Display(double radius, double PI)
{
double height = 5.5;
double cylinderArea = (2* PI * radius * radius) + (2 * PI * radius * height);
string cylinderAreaString = cylinderArea.ToString();
double cylinderVolume = (PI * radius * radius * height);
string cylinderVolumeString = cylinderVolume.ToString();
return cylinderVolumeString + cylinderAreaString;
}
}
}
Main类保持不变。 谢谢,五
最后编辑时间:
我修改了主类代码,所以现在有Console.WriteLine这样的代码:
Shape position = new Shape();
Console.WriteLine(position.Display());
最后一个问题我有,是,当我运行应用程序我得到了圆区和圆柱区的相同编号。它看起来像Cylinder类中的Display方法使用Circle类的返回值。这怎么可能?
你说你はnt来知道你错在哪里。你认为什么是错的?你没有得到你期望的答案吗? –
创建圆柱体类时,应该考虑构图,而不是重复计算圆的面积的代码。圆柱可以表示为圆形和高。然后使用圆的属性来计算圆柱的体积和面积 –
@Rune,我也这么认为,但是指令告诉他'Cylinder'类只继承了来自'Shape'的坐标。可能它的措辞很糟糕。 –