简体   繁体   English

Java隐藏:客户端类可以通过什么方式调用`super`?

[英]Java Hiding: Any Way for Client Classes to Call `super`?

Aside from adding a method explicitly on the subclass to call a super method, is there anyway to "unhide" it temporarily? 除了在子类上显式添加一个方法来调用一个super方法之外,还是要暂时“取消隐藏”它吗? I would like to call the super.blah() method on a Test2 instance. 我想在Test2实例上调用super.blah()方法。 Must I use a method like originalBlah , or is there another way? 我必须使用像originalBlah这样的方法,还是有另一种方式?

public class TestingHiding {
    public static void main(String[] args) {
        Test1 b = new Test2();
        b.blah();
    }
}

class Test2 extends Test1 {
    public void blah() {
        System.out.println("test2 says blah");
    }

    public void originalBlah() {
        super.blah();
    }
}

class Test1 {
    public void blah() {
        System.out.println("test1 says blah");
    }

}

Edit: Sorry to add to the question late in the game, but I had a memory flash (I think): in C# is this different? 编辑:很抱歉在游戏后期添加问题,但我有一个内存闪存(我认为):在C#中这是不同的? That it depends on which class you think you have (Test1 or Test2)? 这取决于你think你拥有哪个班级(Test1或Test2)?

不,没有办法像这样颠覆继承的概念。

As I understand you want to do something like Test1 b = new Test2(); b.super.blah() 据我所知,你想做一些像Test1 b = new Test2(); b.super.blah() Test1 b = new Test2(); b.super.blah() . Test1 b = new Test2(); b.super.blah() This cannot be done, super is strictly limited to calling from inside the subclass to the superclass. 这是不可能的,super严格限于从子类内部调用到超类。

It is hard to imagine situation when you need thing like this. 当你需要这样的东西时很难想象情况。

You can forbid method overriding. 您可以禁止方法覆盖。
Also you can consider Template Method pattern technique. 您还可以考虑模板方法模式技术。

Simple answer is : you can't 简单的答案是:你做不到

you could have blah be 你可能会有

class Test2 extends Test1 {
    public void blah() {
        if("condition"){
            super.blah()
        }
        System.out.println("test2 says blah");
    }

}

or go the originalBlah way, or decouple the blah implementation altogether to be able to call whichever implementation you need but that's about it. 或者采用原始的方式,或者完全解除blah实现,以便能够调用您需要的任何实现,但这就是它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法在C#中调用super的super? - Is there a way to call super's super in C# 是否有针对主要/进入类及其超类的命名约定? - Are there any naming conventions for primary/entrant classes and their super classes? 有什么方法可以访问“ super”类的字段(未继承) - Is there any way to access fields of 'super' class (not inherited) 是否要求子类调用super.doSomething()(如果重写)? - Require child classes to call super.doSomething() if overrides it? 强制客户端代码调用Service类而不是Repository类 - Force client code to call Service classes instead of Repository classes 有什么方法可以在 MudBlazor 的 MudTable 中切换隐藏/显示表列 - Is there any way to toggle hiding/showing a table column in MudBlazor's MudTable 有什么办法不叫控制事件? - Is there any way not to call Control Event? 如果我有一个包含子类A和B的父类A的数组,则有什么方法可以从列表中调用A和B专有的属性 - If i have a Array of parent Class A which contains child classes A and B is there any way i can call properties exclusive to A and B from the list 有没有办法将这些几乎相同的类合并为一个? - Is there any way to combine these almost identical classes into one? 有没有办法在GLSL中使用复杂/派生类? - Is there any way to use complex/derived classes in GLSL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM