如何调用基类方法?

发布于 2024-09-25 02:06:38 字数 636 浏览 2 评论 0原文

假设我有这样声明的类:

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 技术交流群。

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

发布评论

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

评论(4

浮萍、无处依 2024-10-02 02:06:38

你不能。最好的选择是向 PreferenceCategory 添加另一个方法,该方法调用 super 的 validate() 方法。

public boolean validateSuper() {
    return super.validate();
}

但你为什么要这么做呢?这有点设计的味道。您可能会发现责任链模式很有趣。

You can't. Your best bet is to add another method to PreferenceCategory which calls super's validate() method.

public boolean validateSuper() {
    return super.validate();
}

But why would you like to do that? This is a bit a design smell. You may find the chain of responsibilty pattern interesting.

香橙ぽ 2024-10-02 02:06:38

你可以,但只能在子类中:

public boolean simpleValidate() {
    return super.validate();
}

如果你不想重写,为什么不以不同的方式命名该方法呢?如果调用者必须能够选择他们调用的方法,那么这些方法会执行不同的操作,这应该反映在方法名称中。

You can, but only in the subclass:

public boolean simpleValidate() {
    return super.validate();
}

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.

原来分手还会想你 2024-10-02 02:06:38

如果你强制转换,它仍然会使用覆盖方法。所以你应该做类似的事情...

public class PreferenceCategory extends IdentifiableEntity {
    public boolean validate() {
        return true;
    }
    public boolean validateSuper(){
        return super.validate();
    }
}

然后你调用 validatesSuper,它应该可以工作,bot 远不是好的 OO 编程,我真的不建议你这样做;
如果您需要调用不同的验证,那么您应该为该方法指定一个不同的名称,并在需要时调用它,或者立即调用验证来调用超类方法,而不是重写,就像这样...

public class PreferenceCategory extends IdentifiableEntity {
    public boolean validatePreferenceCategory() {
        return true;
    }
}

您仍然可以调用验证来自超类

If you cast, it will still use the override method. So you should do something like that...

public class PreferenceCategory extends IdentifiableEntity {
    public boolean validate() {
        return true;
    }
    public boolean validateSuper(){
        return super.validate();
    }
}

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...

public class PreferenceCategory extends IdentifiableEntity {
    public boolean validatePreferenceCategory() {
        return true;
    }
}

you will still can call validade from superclass

秉烛思 2024-10-02 02:06:38

在 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

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