你能在继承树中重新创建一个方法摘要吗? [英] Can you re-make a method abstract in the inheritance tree?

查看:139
本文介绍了你能在继承树中重新创建一个方法摘要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:

要明确:设计非常丑陋的事实并不重要。关键是,设计就在那里,我必须添加另一个 FlyingMotorizedVehicle 的子类,如果我忘了添加它,这将无法正常工作 FOO(...)。所以我只是想知道我是否可以将其重新定义为抽象。

To be clear: The fact that the design is quite ugly is not the point. The point is, that the design is there and I am in the situation to have to add another sub-class of FlyingMotorizedVehicle which would not work as expected if I forgot to add the foo(...). So I just was wondering if I could redefine it as abstract.

我现在面对一个非常奇怪的继承情况。
可以说,我有三个班级,汽车 MotorizedVehicle FlyingMotorizedVehicle 以及一堆类飞机直升机,......:

I am right now facing a quite weird inheritance situation. Lets say, I have three classes, Vehicle, MotorizedVehicle and FlyingMotorizedVehicle as well as a bunch of classes Airplane, Helicopter, ...:

public abstract class Vehicle {

    abstract Something doStuff(...);

    abstract SomethingElse doOtherStuff(...);

    abstract Foo bar(...);

}

public class MotorizedVehicle extends Vehicle {

    @Override
    Something doStuff(...) {
        return new Something();
    }

    @Override
    SomethingElse doOtherStuff(...) {
        return new SomethingElse();
    }

    @Override
    Foo bar(...) {
        return new Foo();
    }

}

public class FlyingMotorizedVehicle extends MotorizedVehicle {

    @Override
    SomethingElse doOtherStuff(...) {
        return new SomethingElse();
    }

}

public class Airplane extends FlyingMotorizedVehicle  {

    @Override
    Foo bar(...) {
        //do something different
        return new Foo();
    }

}

public class Helicopter extends FlyingMotorizedVehicle {

    @Override
    Foo bar(...) {
        //do something totally different
        return new Foo();
    }

}
[...]

所以 Vehicle 是一个提供一些抽象方法的抽象类。 MotorizedVehicle Vehicle 的子类,具有其方法的具体实现。 FlyingMotorizedVehicle 再次是 MotorizedVehicle 的子类,重写 MotorizedVehicle <的子集的实现/ code> s方法。

So Vehicle is an abstract class providing some abstract methods. MotorizedVehicle is a sub-class of Vehicle with concrete implementations of its methods. FlyingMotorizedVehicle again is a sub-class of MotorizedVehicle overriding the implementations of a subset of MotorizedVehicles methods.

现在有子类 Helicopter 飞机以及可能的其他一些例子覆盖了 MotorizedVehicle#bar(...)的具体实现。
我想要的是强制 MotorizedVehicle 的每个子类必须覆盖栏(...)方法并提供自己的实现。

Now there are the sub-classes Helicopter, Airplane and potentially some others which in the example override the concrete implemenatation of MotorizedVehicle#bar(...). What I want is to "force" every sub-class of MotorizedVehicle to have to override the bar(...) method and provide its own implementation.

是否可以更改 FlyingMotorizedVehicle 以下方式:

Is it possible to just change the FlyingMotorizedVehicle in the following way:

public class FlyingMotorizedVehicle extends MotorizedVehicle {

    @Override
    SomethingElse doOtherStuff(...) {
        return new SomethingElse();
    }

    abstract Foo bar(...);

}

因此我只需重新定义 bar(...)作为抽象方法?我的IDE并没有抱怨它,但这当然并不意味着它会真正起作用。

So that I just redefine the bar(...) as abstract method? My IDE is not complaining about it, but that of course does not mean, that it will actually work.

我希望你能得到我在这里指出的内容。

I hope you get what I try to point out here.

提前致谢

Bluddy

推荐答案

是的,您必须将栏(...)重新定义为抽象方法。
然后你必须声明公共类FlyingMotorizedVehicle 作为抽象类

Yes, You have to redefine the bar(...) as abstract method. Then you have to declare public class FlyingMotorizedVehicle as a abstract class as well

public abstract class FlyingMotorizedVehicle extends MotorizedVehicle {

    @Override
    SomethingElse doOtherStuff(...) {
        return new SomethingElse();
    }

    abstract Foo bar(...);

}

这篇关于你能在继承树中重新创建一个方法摘要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆