gpt4 book ai didi

C#抽象类继承

转载 作者:行者123 更新时间:2023-11-30 22:34:29 25 4
gpt4 key购买 nike

伙计们,

我正在尝试解决这个作业。我已经走到了死胡同,我正在寻找可以帮助解决这个问题的人。

这是我要解决的任务:

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("Car with four wheels.");
}
}
}

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("Sport version of the car.");
}
}
}

类(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.

编辑:我的问题是:如何更改我编写的代码来完成我被赋予的任务?因为我不确定在编写代码时如何通用。当我将 car 属性添加到 Vehicle4Wheels 类时,例如:

private double carWeight;

我怎样才能在以后的派生类中处理这个属性?

最佳答案

其中一些值(highestSpeed、carWeight 等)可以应用于任何“4 轮车辆”(甚至“车辆”),因此您可以为这些值定义属性 < em>那个水平。

然后在(派生类的)构造函数中,您可以为这些属性提供值。

Display 方法随后可以读取这些属性,因此您甚至不必在更高级别覆盖该方法。

另见 http://msdn.microsoft.com/en-us/library/ms173115.aspx ,关于构造函数以及如何从派生类的构造函数调用基类的构造函数。

关于C#抽象类继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7804713/

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