简体   繁体   English

如何从父级父类调用方法?

[英]How can you invoke a method from a grand parent class?

There was a question in the exam that says 考试中有一个问题说

If class A extends Class B and B extends Class C. And they all have the method test() 如果类A扩展了类B,而类B扩展了类C。它们都具有方法test()

How to invoke the method test that in class C in class A ? 如何在A类的C类中调用测试方法?

I stupidly chose. 我愚蠢地选择了。 Super.super.test(). Super.super.test()。 Which I think it's wrong. 我认为这是错误的。

Th other options were: 其他选项是:

Test() Super.test(). Test()Super.test()。 I thought about this. 我想到了 But if I used this it will invoke the method in class B and class B will invoke the method in Class C But we only want C 但是,如果我使用它,它将在B类中调用该方法,而B将在C类中调用该方法,但是我们只希望C

Sorry if this is not very clear or not organized just finish exam and kinda dizzy. 很抱歉,如果这不是很清楚或组织不清,请完成考试并感到头晕。

There is no way you access the method of super - super class. 您无法访问超级类的方法。 If the class B override the automatically when you run the super will run both the instructions contained in the class B, as in class C. Now if you ensure that there is no instruction implemented in class B, you will automatically execute only the instructions of the class C. 如果B类在运行超级控件时自动覆盖了B类,则将像B类一样运行B类中包含的两条指令。现在,如果确保B类中未实现任何指令,则将仅自动执行B类中的指令。 C级

If the class B has instructions that you did not want to run, duplicate the method by changing the name and run from the class A using the identifier super. 如果类B包含您不想运行的指令,请通过更改名称来复制方法,并使用标识符super从类A运行。

It is not possible to do so. 不可能这样做。 Only through invoking the superclass method, which in turn invokes the method in its superclass (C) 仅通过调用超类方法,该方法继而在其超类(C)中调用该方法

This is possible in C++ but not in Java. 这在C ++中是可能的,但在Java中是不可能的。 super always redirects you to immediate parent. super总是将您重定向到直接父级。

The super.super() is not allowed in java. 在Java中不允许super.super() Try it this way if you really want , you can implement that by yourself : 如果您真的想尝试这种方式,则可以自己实现:

public class abc {  
        public void myName(){  
            System.out.println("abc");  
        }  
    }  

    public class def extends abc {  
        public void myName(boolean flag){  
            if(flag){   
                super.myName();  
            }else {  
                System.out.println("def");  
            }  
        }  
    }  



    public class mno {
        public static void main(String[] args){
            def d = new def();

            System.out.print("This returns the so called 'super.super' : ");
            d.myName(true);

        }
    }

output : This returns the so called 'super.super' : abc 输出 :这将返回所谓的“ super.super”:abc

I would imagine the below would print out "A". 我想下面将打印出“ A”。 I do not have a compiler convenient to try it out. 我没有方便尝试的编译器。

class A
{
     private void test ( ) { System . out . println ( "A" ) ; }

     public static void main ( String [ ] args )
     {
          C c = new C ( ) ;
          c . test ( ) ;
     }
}


class B extends A
{
     private void test ( ) { System . out . println ( "A" ) ; }
}


class C extends B
{
     private void test ( ) { System . out . println ( "A" ) ; }
}

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

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