gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:41:26 25 4
gpt4 key购买 nike

我的最后一个问题是 C# 中的继承问题。我以为我理解了这个主题,但不知怎的,我错过了为什么输出是这样的。

这是我的类(class):

基类:

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();

}
}

车辆四轮类:

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

类(class)集会:

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()不是构造函数。)

对于虚拟方法(请注意,抽象方法自动变为虚拟方法),“最派生”类的方法是被调用的方法,并且仅调用该方法 - 除非该方法调用base.MethodName().

关于c# - 控制台中派生类的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7634660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com