- class Super
- {
- int i = 20;
- public Super(String a)
- {
- i=1;
- }
- public Super()
- {
- System.out.println("b "+i);//b 20
- i += 2;
- System.out.println("c "+i);//c 22
- }
- }
- class Demo extends Super
- {
- public Demo (String a)
- {
- System.out.println("d "+i);//d 22
- i = 5;
- System.out.println("e "+i);//e 5
- }
- public static void main(String[] args)
- {
- int i = 4;
- System.out.println("a "+i);//a 4
- Super d=new Demo("A");
- System.out.println("f "+i);//f 4
- System.out.println(d.i);//5
- }
- }
复制代码
- class Super
- {
- int i = 20;
- public Super(String a)
- {
- i=1;
- }
- public Super()
- {
- System.out.println("a "+i);//a 20
- i += 2;
- System.out.println("b "+i);//b 22
- }
- }
- class Demo extends Super
- {
- static int i = 4;
- public Demo (String a)
- {
- System.out.println("c "+i);//c 4
- i = 5;
- System.out.println("d "+i);//d 5
- }
- public static void main(String[] args)
- {
- Super d=new Demo("A");
- System.out.println("e "+i);//e 5
- System.out.println(d.i);//22
- }
- }
复制代码
一个结果是5
一个结果是22为什么是这个结果呢?
|