本帖最后由 李培根 于 2012-12-2 20:29 编辑  
 
11.写出程序结果: class Fu{         int num = 4;         void show(){                 System.out.println("showFu"):         } } class Zi extends Fu{         int num = 5;         void show(){                 System.out.println("show Zi");         } } class T{         public static void main(String[] args){                 Fu f = new Zi();                 Zi z = new Zi();                 System.out.println(f.num);                 System.out.println(z.num);                 f.show();                 z.show();         } } 4  5  showzi  showzi 成员变量看左边。 成员函数编译左边,运行右边。 ----------------------------------------------------- 12. interface A{         void show(); } interface B{         void add(int a,int b); } class Cimplements A,B{         //程序代码         private int sum;         public void add(int a,int b){                 sum = a+b;                                 }                 void show(){                 System.out.println(sum);         }          } class D{         public static void main(String[] args){                 C c = new C();                 c.add(4,2);                 c.show();//通过该函数打印以上两个数的和。         } } ----------------------------------------------------- 13.写出程序结果。 class Demo{         public static void main(String[] args){                 try{                         showExce();                         System.out.println("A");                 }                 catch(Exception e){                         System.out.println("B");                                         }                 finally{                         System.out.println("C");                 }                 System.out.println("D");         }         public static void showExce() throws Exception{                 throw new Exception;         } } 打印结果:B         C        D ----------------------------------------------------- 14.写出程序结果。 class Super{         int i = 0;         public Super(String s){                 i = 1;         } } class Demo extends Super{         public Demo(String s){                 i = 2;         }         public static void main(String[] void){                 Demo d = new Demo("yes");                 System.out.println(d.i);         } } 编译失败,父类中没有空参数的构造函数。 ----------------------------------------------------- 
 15.写出程序结果。 class Super{         public int get(){return 4;} } class Demo extends Super{         public long get(){return 5;}         public static void main(String[] args){                 Super s = new Demo();                 System.out.println(s.get());         } } 覆盖失败,编译失败。 ----------------------------------------------------- 16.写出程序结果。 class Demo{         public static void func(){                 try{                         throw new Excception();                         System.out.println("A");//该条语句无法被执行,废话。                 }                 catch(Exception e){                         System.out.println("B”);                 }         }         public static void main(String[] args){                 try{                         func();                 }                 catch(Exception e){                         System.out.println("C");                 }                 System.out.println("D");         } } ----------------------------------------------------- 17. class Demo{         public void func(){                 //位置1:new Inner();         }         class Inner{}         public static void main(String[] args){                         Demo d = new Demo();                         //位置2:new Inner(); 不可以。因为主函数是静态的,只能调用静态成员,所以内部类也必须是static的。                         new d.Inner();//new new Demo().Inner();格式错误  new Demo().new Inner();                         new Demo.Inner();//格式正确的。但是Inner必须是静态的。                                  }                 } A.在位置1写 new Inner(); B.在位置2写 new Inner(); C.在位置2写new d.Inner(); D.在位置2写new Demo.Inner(); ----------------------------------------------------- 18.写出程序结果 class Exc0 extends Exception{} class Exc1 extends exc0{} class Demo{         public static void main(String[] args){                 try{                         throw new Exc1();                 }                 catch(Exception e){//多catch是,父类的catch放在最下面。                         System.out.println("Exception");                 }                 catch(Exc0 e){                         System.out.println("Exc0");                 }         } } 编译失败。 ----------------------------------------------------- 19. interface Test{         void func(); } class Demo{         public static void main(String[] args){                 //补足代码:(匿名内部类)调用show方法。                 new Demo().show(new Test(){                         public void func(){}                 });         }         void show(Test t){                 t.func();         }         } ----------------------------------------------------- 20.写出程序结果 class Test{         public static String output="";         public static void foo(int i){                 try{                         if(i==1)                                 throw new Exception();                         output+="1";                 }                 catch(Exception e){                         output+="2";                         return;                 }                 finally{                         output+="3";                 }                 output+="4";         }         public static void main(String args[]){                 foo(0);                 System.out.println(output);//134                 foo(1);                 System.out.println(output);//13423         } }      |