1 package thinkingDemo; 2 /* 3 * 4 * 关于嵌套类覆盖问题 5 * 6 */ 7 class Egg2 { 8 protected class Yolk{ 9 public Yolk(){ 10 System.out.println("Egg2.Yolk()"); 11 } 12 public void f(){ 13 System.out.println("Egg2.Yolk.f()"); 14 } 15 } 16 private Yolk y = new Yolk() ; 17 public Egg2(){ 18 System.out.println("New Egg2()"); 19 } 20 public void insertYolk(Yolk yy){ 21 y = yy ; 22 } 23 public void g(){ 24 y.f() ; 25 } 26 } 27 class BigEgg2 extends Egg2 { 28 public class Yolk extends Egg2.Yolk { 29 public Yolk(){ 30 System.out.println("BigEgg2.Yolk()"); 31 } 32 public void f(){ 33 System.out.println("BigEgg2.Yolk.f()"); 34 } 35 } 36 public BigEgg2(){ 37 insertYolk(new Yolk()) ; 38 } 39 } 40 public class NestClassOverride { 41 public static void main(String[] args) { 42 Egg2 e2 = new BigEgg2() ; 43 e2.g() ; 44 } 45 }
执行结果:
Egg2.Yolk()
New Egg2()
Egg2.Yolk()
BigEgg2.Yolk()
BigEgg2.Yolk.f()
由此可得: 内部类不会被覆盖
时间: 2025-01-14 00:21:02