моя последняя проблема с наследованием в 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 #)?
Спасибо за ответы.
EDIT:
Извините, мне потребовалось некоторое время, чтобы вернуться к этой теме. Чтобы быть более точным, я опубликую задачу домашней работы, которую я пытаюсь выполнить:
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.