📜  Java |继承|问题8

📅  最后修改于: 2021-06-29 02:56:32             🧑  作者: Mango

预测以下Java程序的输出

// filename Main.java
class Grandparent {
    public void Print() {
        System.out.println("Grandparent's Print()");
    }
}
   
class Parent extends Grandparent {
    public void Print() {
        System.out.println("Parent's Print()");
    }
}
   
class Child extends Parent {
    public void Print() {
        super.super.Print(); 
        System.out.println("Child's Print()");
    }
}
   
public class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.Print();
    }
}

(A) super.super.Print()中的编译器错误
(B)

Grandparent's Print()
Parent's Print()
Child's Print()

(C)运行时错误答案: (A)
说明:在Java,不允许执行super.super。我们只能使用父级访问祖父母的成员。例如,以下程序可以正常运行。

// Guess the output
// filename Main.java
class Grandparent {
    public void Print() {
        System.out.println("Grandparent's Print()");
    }
}
 
class Parent extends Grandparent {
    public void Print() {
        super.Print();  
        System.out.println("Parent's Print()");
    }
}
 
class Child extends Parent {
    public void Print() {
        super.Print();  
        System.out.println("Child's Print()");
    }
}
 
class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.Print();
    }
}

这个问题的测验