简体   繁体   English

Java调用子类的前父方法

[英]Java calling pre-parent method of child class

In c++ you can easely call eny oveloaded method from any previus parent classes. 在c ++中,您可以从任何普通的父类中轻松调用eny oveloaded方法。 How an i do somethink like that in Java? 我是如何在Java中做那样思考的?

Example: 例:

class A {

    public void f() {
        System.out.println("A");
    }

};

class B extends A {

    @Override
    public void f() {  
        super.f();
        System.out.println("B");
    }

};

class C extends B {

    @Override
    public void f() {  
        super.f(); // how to invoke f() method of A class avoiding B class?
        System.out.println("C");
    }

};

So calling cf() will print "ABC". 因此调用cf()将打印“ABC”。

How to call from C class method of A class avoiding same overloaded method of B class? 如何从A类的C类方法调用避免B类的相同重载方法?

In Java you can't do this without a hack because C is a B not just an A. If you can do this in C++, it's a bad idea IMHO. 在Java中,你不能没有黑客攻击,因为C不仅仅是A而不是A.如果你能在C ++中做到这一点,那么恕我直言! This suggests your hierarchy is messed up and C should be only an A not a B. 这表明您的层次结构搞砸了,C应该只是A而不是B.

If you really have to do this you can add a method to B like this so you can bypass Bf(); 如果你真的必须这样做,你可以像这样添加一个方法,这样你就可以绕过Bf();

protected void superF() {
    super.f();
}

and call this from C. 并从C调用此


It is possible to generate the byte code using ASM to do this. 可以使用ASM生成字节代码来执行此操作。 The byte code allows you to call any level of parent, it is Java which does not. 字节代码允许您调用任何级别的父级,而Java则不会。

As others have already pointed out, you can't get it directly (For example super.super. doesn't work). 正如其他人已经指出的那样,你不能直接得到它(例如super.super.不起作用)。

Here is a hack you can mess around with: 这是一个你可以搞乱的黑客

try
{
    Class<?> s = this.getClass();
    while ( !"java.lang.Object".equals(s.getSuperclass().getName())  )
    {
        s = s.getSuperclass();
    }
    Method m = s.getMethod(Thread.currentThread().getStackTrace()[1].getMethodName(), (Class<?>[])null);
    if ( m != null )
    {
        m.invoke(s.newInstance()); // This is the actual call to the 'super' method.
    }
}
catch ( Exception e )
{
    e.printStackTrace();
}

It's not pretty, but for simple classes and methods it works ;) 它不漂亮,但对于简单的类和方法,它的工作原理;)

ps. PS。 This hack only supports methods without any parameters. 这个hack只支持没有任何参数的方法。 If you need to support parameters you'll need to use the getMethods() method. 如果需要支持参数,则需要使用getMethods()方法。 Also, if the final "superclass" doesn't support the method it'll also not work, but you can add code in the while loop to check that and catch the last superclass that does support it. 此外,如果最终的“超类”不支持该方法,它也将无法工作,但您可以在while循环中添加代码来检查并捕获支持它的最后一个超类。

No this can't be done in java. 不,这不能在java中完成。 However if you want it to be done create an instance of class A in overridden method f() of class C and invoke f() of class A using the object. 但是,如果要完成它,请在类C的重写方法f()中创建类A的实例,并使用该对象调用类A的f()。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM