如何调用基类方法?
假设我有这样声明的类:
public abstract class IdentifiableEntity {
public boolean validate() {
return true;
}
}
public class PreferenceCategory extends IdentifiableEntity {
public boolean validate() {
return true;
}
}
现在,假设我创建了 PreferenceCategory 变量,并且我想调用 IdentifyingEntity.validate() 方法,而不是 PreferenceCategory.validate() 方法。
我本以为我可以通过强制转换来做到这一点(见下文),但它仍然调用重写的方法:
PreferenceCategory cat = new PreferenceCategory();
// this calls PreferenceCategory.validate(), not what I want
((IdentifiableEntity)cat).validate();
有什么方法可以做到这一点吗?
Say I have classes declared like this:
public abstract class IdentifiableEntity {
public boolean validate() {
return true;
}
}
public class PreferenceCategory extends IdentifiableEntity {
public boolean validate() {
return true;
}
}
Now, let's say I have PreferenceCategory variable created, and I want to call the IdentifiableEntity.validate() method, not the PreferenceCategory.validate() method.
I would have thought I could do this with a cast (see below), but it still calls the overridden method:
PreferenceCategory cat = new PreferenceCategory();
// this calls PreferenceCategory.validate(), not what I want
((IdentifiableEntity)cat).validate();
Is there any way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你不能。最好的选择是向
PreferenceCategory
添加另一个方法,该方法调用 super 的validate()
方法。但你为什么要这么做呢?这有点设计的味道。您可能会发现责任链模式很有趣。
You can't. Your best bet is to add another method to
PreferenceCategory
which calls super'svalidate()
method.But why would you like to do that? This is a bit a design smell. You may find the chain of responsibilty pattern interesting.
你可以,但只能在子类中:
如果你不想重写,为什么不以不同的方式命名该方法呢?如果调用者必须能够选择他们调用的方法,那么这些方法会执行不同的操作,这应该反映在方法名称中。
You can, but only in the subclass:
If you don't want overriding, why not name the method differently? If callers must be able to choose the method they invoke, the methods do different things, which should be reflected in the method name.
如果你强制转换,它仍然会使用覆盖方法。所以你应该做类似的事情...
然后你调用 validatesSuper,它应该可以工作,bot 远不是好的 OO 编程,我真的不建议你这样做;
如果您需要调用不同的验证,那么您应该为该方法指定一个不同的名称,并在需要时调用它,或者立即调用验证来调用超类方法,而不是重写,就像这样...
您仍然可以调用验证来自超类
If you cast, it will still use the override method. So you should do something like that...
Then you call validatesSuper, it should work, bot is far from good OO programming, and I really do not recommend you to do that;
If you need to call a different validate so you should just give a different name for that method and call it when you need, or call validate to invoke the superclass method now, not overrided, like this...
you will still can call validade from superclass
在 Java 中没有办法做到这一点,除非在子类中使用 super.methodName()。即使有反思也不行。其他语言(例如 Ruby)可以通过反射来完成。
http://blogs.oracle.com/sundararajan/entry/calling_overriden_superclass_method_on
There is no way to do that in Java, except from within the subclass with super.methodName(). Not even with reflection. Other languages such as Ruby can do it with reflection.
http://blogs.oracle.com/sundararajan/entry/calling_overriden_superclass_method_on