你能在继承树中重新制作一个方法抽象吗? [英] Can you re-make a method abstract in the inheritance tree?

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

问题描述

需要说明的是:设计很丑的事实并不是重点.关键是,设计就在那里,我不得不添加另一个 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.

我现在面临着一个非常奇怪的继承情况.假设,我有三个类,VehicleMotorizedVehicleFlyingMotorizedVehicle 以及一堆类 Airplane直升机, ...:

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 是一个抽象类,提供一些抽象方法.MotorizedVehicleVehicle 的子类,具有其方法的具体实现.FlyingMotorizedVehicle 再次是 MotorizedVehicle 的子类,覆盖了 MotorizedVehicle 方法子集的实现.

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.

现在有子类 HelicopterAirplane 和可能的其他一些子类,它们在示例中覆盖了 MotorizedVehicle#bar(...).我想要的是强制" MotorizedVehicle 的每个子类必须覆盖 bar(...) 方法并提供自己的实现.

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.

提前致谢

混蛋

推荐答案

是的,您必须将 bar(...) 重新定义为抽象方法.然后你必须将public class 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天全站免登陆