Voj*_*ech 2 .net c# inheritance
我的最后一个问题是C#中的继承.我以为我理解了这个话题,但不知怎的,我错过了为什么输出是这样的.
这是我的课程:
BaseClass的:
public abstract class Vehicle
{
public Vehicle()
{
Console.WriteLine("Honda Civic");
}
public abstract void Display();
}
Run Code Online (Sandbox Code Playgroud)
衍生的1级:
public class Vehicle4Wheels : Vehicle
{
public override void Display()
{
Console.WriteLine("Derived111 class Constructor.");
}
}
Run Code Online (Sandbox Code Playgroud)
派生类2:
public class SportCar : Vehicle4Wheels
{
public new void Display()
{
Console.WriteLine("Derived222 class Constructor.");
base.Display();
}
}
Run Code Online (Sandbox Code Playgroud)
这是层次结构:基类 - >派生类1 - >派生类2
这是我得到的输出:
Honda Civic
Derived222 class Constructor.
Derived111 class Constructor.
Run Code Online (Sandbox Code Playgroud)
这是我想要实现的输出:
Honda Civic
Derived111 class Constructor.
Derived222 class Constructor.
Run Code Online (Sandbox Code Playgroud)
我已经阅读了几篇文章,其中声明基类首先打印,其他派生类根据它们在层次结构中的位置打印.
那么为什么最后一个派生类在第一个派生类之前打印出来?我缺少什么(除了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.
Run Code Online (Sandbox Code Playgroud)
类车辆:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
Class 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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
Class 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.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
班级拉力赛:
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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定如何用抽象方法实现任务的目标.谢谢你的答案,V.
您正在将构造函数的概念与虚方法的概念混合在一起.确实按照从base到derived的顺序调用构造函数,但是你创建了非构造函数的虚方法.
这将提供您想要的输出:
// In Vehicle4Wheels
public Vehicle4Wheels()
{
Console.WriteLine("Vehicle4Wheels constructor");
}
// In SportCar
public SportCar()
{
Console.WriteLine("SportCar constructor");
}
Run Code Online (Sandbox Code Playgroud)
(另外,编辑您在打印字符串Display()方法,因为它们是误导性的- Display()是不是一个构造函数).
对于虚方法(注意abstract方法自动变为虚方法),"最派生的"类方法是被调用的方法,只调用该方法 - 除非方法调用base.MethodName().