java 需要覆盖非抽象类的特定方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14272026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Require override of specific methods of a non-abstract class
提问by brain56
Is it possible to have a class defined like
是否有可能定义一个类
public class MyClass
{
public void methodA(){} // Inherit
public void methodB(){} // Inherit
public void methodC(){} // Require override
}
and then have all classes which extend from MyClass to be required to override methodC()
but just simply inherit methodA()
and methodB()
?
然后让所有从 MyClass 扩展的类都需要覆盖methodC()
但只是简单地继承methodA()
和methodB()
?
If it is possible, how does one do it? If it's not possible, can you propose an alternative solution to achieve a similar result?
如果可能,如何实现?如果不可能,您能否提出替代解决方案来实现类似的结果?
EDIT:
编辑:
I need a non-abstract class because I want to be able to instantiate this class too.
我需要一个非抽象类,因为我也希望能够实例化这个类。
采纳答案by Thilo
You cannot require an override of a non-abstract method.
您不能要求覆盖非抽象方法。
Maybe you can do something similar to the template method pattern:
也许你可以做一些类似于模板方法模式的事情:
public final void methodC() { methodC1(); someMoreLogic(); methodC2();}
protected abstract void methodC1();
protected abstract void methodC2();
Here methodC encapsulates a fixed algorithm that calls into pieces that have to be supplied by the subclasses.
这里 methodC 封装了一个固定的算法,该算法调用必须由子类提供的部分。
回答by EJK
You would have to make your base class abstract.
你必须使你的基类抽象。
public abstract class MyClass
{
public void methodA(){} // Inherit
public void methodB(){} // Inherit
public abstract void methodC(); // Require override
}
回答by Patricia Shanahan
I don't think you do exactly what you want. Alternatively, create MyBaseClass as an abstract class with methodC()
abstract implementations for methodA()
and methodB(). Derive MyClass from it, adding an implementation for methodC()
. Any classes that you do not want to have inherit that implementation should directly subclass MyBaseClass rather than MyClass.
我不认为你做的正是你想要的。或者,将 MyBaseClass 创建为抽象类,并具有methodC()
formethodA()
和 methodB() 的抽象实现。从中派生 MyClass,为methodC()
. 您不希望继承该实现的任何类都应直接子类化 MyBaseClass 而不是 MyClass。
回答by Juvanis
If you want a method to be just inherited use final
keyword. To force overriding make it abstract
. However, only non-abstract child classes will have to override it.
如果您只想继承一个方法,请使用final
关键字。强制覆盖使它abstract
。但是,只有非抽象子类必须覆盖它。
回答by NPKR
AFAIK there is no way to force override a method in Java with out abstract.
AFAIK 没有办法在没有抽象的情况下强制覆盖 Java 中的方法。
You can achive with abstract
class by making the method as abstract
method.
您可以abstract
通过创建method as abstract
方法来实现类。
回答by funny
one way to do it may be to use virtual keyword instead of abstract but it doesnt requireto override, but you cando override. though so, you can instantiate.
一种方法可能是使用 virtual 关键字而不是 abstract ,但它不需要覆盖,但您可以覆盖。尽管如此,您可以实例化。
another way and more recommended is to create an interface where you can indicate what the class requirements. it is something like abstract, you indicate what to require, but keep in mind interface is not a class. it's more like pointer. for more for interface click: https://docs.oracle.com/javase/tutorial/java/concepts/interface.html
另一种更推荐的方法是创建一个界面,您可以在其中指出类的要求。它类似于抽象,您指出需要什么,但请记住接口不是类。它更像是指针。更多界面请点击:https: //docs.oracle.com/javase/tutorial/java/concepts/interface.html