控制台中派生类的顺序 - c#

我的最后一个问题是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();

}
}

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");
    }
}
}

跑车类:

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。

参考方案

您正在将构造函数的概念与虚拟方法的概念混合在一起。确实按从基到派生的顺序调用了构造函数,但是您已经创建了非构造函数虚拟方法。

这将提供您想要的输出:

// In Vehicle4Wheels
public Vehicle4Wheels()
{
    Console.WriteLine("Vehicle4Wheels constructor");
}

// In SportCar
public SportCar()
{
    Console.WriteLine("SportCar constructor");
}

(还请在Display()方法中编辑要打印的字符串,因为它们会引起误解-Display()不是构造函数。)

对于虚拟方法(请注意,abstract方法会自动变为虚拟方法),“最派生的”类方法是被调用的方法,只有该方法才被调用-除非该方法调用base.MethodName()

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

改造正在返回一个空的响应主体 - java

我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…

System.out.printf不打印整数参数 - java

我是Java编程的新手,无法从另一个类返回方法。这两个类都可以编译并成功运行。我可以从一个类中调用一个简单的int,但是当我想计算用户输入的两个输入整数时,我只会得到一个空格。这是我的计算课class calculations { public final int AGE = 53; public int numbers(int num1, int num2…

每个文件合并后添加换行 - python

我有很多类似以下内容的JSON文件:例如。1.json{"name": "one", "description": "testDescription...", "comment": ""} test.json{"name"…

使用python将信息从Linux文件解析到Windows - python

我正在尝试从Linux环境中解析一些内容,然后使用python将它们转储到Windows环境中的excel文件中。eg: foo/bar/myfile.txt我要在Windows env中保留一些Excel文件的内容C:foo\bar\myfile.txt我知道如何提取所需的信息,但找不到从python的Linux env在Windows操作系统中创建文件的…