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'的座標。可能它的措辭很糟糕。 –