时间:2019-03-17 标签:c#abstractclasses

发布于 2024-11-04 03:54:05 字数 216 浏览 1 评论 0原文

我必须创建一个假的 DMV 程序来计算商用和私人车辆的年费。这两个类将是抽象的,并且它们将来自名为“所有车辆”的类的多态性。

我的老师只希望一个对象创建整个程序(在主程序中),但因为我的前 3 层类是抽象的。我无法用它们创建对象,即 cars = new cars();

所以我的问题是我如何只创建一个对象,因为它们是抽象的?如果您有任何疑问,请随时提问,我可能没有解释清楚......

i have to create a fake DMV program that calculates annual fees, for commercial and private vehicles. Those two classes will be abstract and they will polymophism from a class named all vehicles.

My instructor wants only one object created the entire program(in main) but since my top 3 tier classes are abstract. I can't create an object with them i.e. vehicles = new vehicles();

so my question is how do i create only one object since they are abstract? If you have any questions feel free to ask, I might have not explained this well...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

灯下孤影 2024-11-11 03:54:06

也许您要创建一个代表 DMV 的对象,但该对象的定义包括其他对象的实例。

class DMV {
 private List<Vehicle> vehicles = new List<Vehicle>();
 ...
}

Maybe you are to make one object which represents the DMV, but the definition of that object includes instances of other objects.

class DMV {
 private List<Vehicle> vehicles = new List<Vehicle>();
 ...
}
明月松间行 2024-11-11 03:54:06

您的讲师可能希望您通过对抽象基类的引用来实例化多个对象:

Vehicle conveyance;
conveyance = new PrivateVehcicle();
conveyance.Drive();
conveyance.Stop();
// or whatever ...
conveyance = new CommercialVehcicle();
conveyance.Drive();
conveyance.Stop();

即您有一个对象引用(传送),其行为取决于您实例化的具体类型的多态性。

Your instructor may want you to instantiate multiple objects through a reference to the abstract base class:

Vehicle conveyance;
conveyance = new PrivateVehcicle();
conveyance.Drive();
conveyance.Stop();
// or whatever ...
conveyance = new CommercialVehcicle();
conveyance.Drive();
conveyance.Stop();

i.e. you have a single object reference (conveyance) that behaves polymorphically depending on the concrete type you've instantiated.

著墨染雨君画夕 2024-11-11 03:54:05

您的班级结构将类似于:

abstract class Vehicle
{
    protected abstract int BaseFee { get; }
    protected abstract int ExtraFee { get; }

    public int CalculateFee()
    {
        return BaseFee + ExtraFee;
    }
}

abstract class CommercialVehicle : Vehicle
{
    protected override int BaseFee
    {
        return 100;
    }
}

class LightDutyTruck : CommercialVehicle
{
    protected override int ExtraFee
    {
        return 50;
    }
}

class Semi : CommercialVehicle
{
    protected override int ExtraFee
    {
        return 150;
    }
}

[etc...]

abstract class PrivateVehicle : Vehicle
{
    protected override int BaseFee
    {
        return 25;
    }
}

class Sedan : PrivateVehicle
{
    protected override int ExtraFee
    {
        return 15;
    }
}

对于每个班级,依此类推。在 Main 方法中,您将根据输入创建实例,但实际变量将被声明为 Vehicle 类型。实际计算将根据您创建的实例生效:

Vehicle v;
switch(input)
{
    case "semi":
        v = new Semi();
        break;
    case "truck":
        v = new LightDutyTruck();
        break;
    ...
}

int fee = v.CalculateFee();

Your class structure will look something like:

abstract class Vehicle
{
    protected abstract int BaseFee { get; }
    protected abstract int ExtraFee { get; }

    public int CalculateFee()
    {
        return BaseFee + ExtraFee;
    }
}

abstract class CommercialVehicle : Vehicle
{
    protected override int BaseFee
    {
        return 100;
    }
}

class LightDutyTruck : CommercialVehicle
{
    protected override int ExtraFee
    {
        return 50;
    }
}

class Semi : CommercialVehicle
{
    protected override int ExtraFee
    {
        return 150;
    }
}

[etc...]

abstract class PrivateVehicle : Vehicle
{
    protected override int BaseFee
    {
        return 25;
    }
}

class Sedan : PrivateVehicle
{
    protected override int ExtraFee
    {
        return 15;
    }
}

and so on, for each class. In your Main method, you would create the instance based on input, but your actual variable would be declared as type Vehicle. The actual calculation will take effect based on the instance that you create:

Vehicle v;
switch(input)
{
    case "semi":
        v = new Semi();
        break;
    case "truck":
        v = new LightDutyTruck();
        break;
    ...
}

int fee = v.CalculateFee();
爱的故事 2024-11-11 03:54:05

你似乎有点困惑。 “所有车辆”应该是抽象的。 “商用车”和“私家车”不应该是抽象的,除非这两者有具体的子类。

您也可能不理解您的老师所说的“只有一个对象”的含义,因为这没有意义。

You seem to be a bit confused. "All Vehicles" should be abstract. "Commercial Vehicle" and "Private Vehicle" should not be abstract, unless there are concrete subclasses of those two.

You may also not be understanding what your instructor means by "only one object", since that doesn't make sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文