我最後的問題是在C#中繼承。我以爲我理解這個話題,但不知何故,我忽略了爲什麼輸出是這樣的。控制檯中派生類的順序
這裏是我的類:
BaseClass的:
public abstract class Vehicle
{
public Vehicle()
{
Console.WriteLine("Honda Civic");
}
public abstract void Display();
}
派生類1:
public class Vehicle4Wheels : Vehicle
{
public override void Display()
{
Console.WriteLine("Derived111 class Constructor.");
}
}
派生類2:
public class SportCar : Vehicle4Wheels
{
public new void Display()
{
Console.WriteLine("Derived222 class Constructor.");
base.Display();
}
}
這是層次結構:基類 - >派生1級 - >派生類2
這是我得到的輸出:
Honda Civic
Derived222 class Constructor.
Derived111 class Constructor.
這是我想達到的輸出:
Honda Civic
Derived111 class Constructor.
Derived222 class Constructor.
我看了幾篇文章的地方有人說,首先打印基類,其他派生類是根據它們在層次結構中的位置打印的。
那麼爲什麼最後一個派生類在第一個派生類之前打印呢?我錯過了什麼(除了C#編程技巧)?
謝謝你的答案。
編輯:
我很抱歉,我花了一段時間才能回到這個主題。更精確地說,我會發布的功課,我想實現的任務:
Work 2:
An abstract class is not a complete class, it misses some parts, and you cannot create
objects from it. The programmer who writes the derived classes must fill in the missing
parts. Consider an abstract class Vehicle. Derive two hierarchies from this class as it
is shown below: Now, write 4 classes, see the yellow rectangle. Start from the abstract
base class Vehicle -> Vehicle with 4 wheels -> Sport Cars and stop at the derived class Rally, which is the most specific
class. The class Vehicle contains a field which holds the vehicle name and an abstract
method void Display().
Implement this function in the derived classes, so that the function returns
information about the vehicle, e.g. the motor power and other necessary properties. The
last derived class has private fields to hold the motor power, the car weight, the car
acceleration, the highest speed and a function that computes the specific power (power
/weight). The function Display returns a text string with all this information. Test
your work in a Console application that uses objects of the type of the classes Sport
car and Rally.
類車輛:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_work_2
{
public abstract class Vehicle
{
public string vehicleName;
public abstract void Display();
}
}
類Vehicle4Wheels:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_work_2
{
public class Vehicle4Wheels : Vehicle
{
public override void Display()
{
Console.WriteLine("Car1");
}
}
}
類SportCar:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_work_2
{
public class SportCar : Vehicle4Wheels {
public override void Display()
{
Console.WriteLine("Derived222 class Constructor.");
}
}
}
類拉力賽:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_work_2
{
public class Rally : SportCar
{
private double motorPower = 408;
private double carWeight = 2380;
private double carAcceleration = 4.7;
private double highestSpeed = 250;
public double SpecificPower()
{
double specificPower = motorPower/carWeight;
return specificPower;
}
public override void Display()
{
Console.WriteLine("The acceleration is: {0}.\nThe highest speed is {1} km/h.", carAcceleration, highestSpeed);
Console.WriteLine("Specific power is {0}", SpecificPower());
}
}
}
我不知道如何用抽象方法實現任務的目標。 謝謝您的回答,V.
您還沒有顯示Vehicle4Wheels ...您已經顯示Vehicle兩次。你的代碼也會*打印*它在構造函數中,而不是來自* actual *構造函數。如果你能想出一個更明智,更完整的例子,那麼幫助你會更容易。 –
編輯你的問題,你錯過了class2的實施 – Boomer
@Boom:請不要使用Leet on SO – abatishchev