分享一個Android筆試題

月盡天明發表於2012-02-20

前幾天去面試,遇到一個關於java繼承的題目,跟大家分享下。大牛就不用看啦,只是檢查一下大家的基礎。題目如下:
有三個類,一父二子,最好呼叫看執行結果:
父類:

  1. 1.public class Father {    
    2.    static {    
    3.        System.out.println("Father static Create");    
    4.    }    
    5.    {    
    6.        System.out.println("Father Create");    
    7.    }    
    8.        
    9.    public static void StaticFunction(){    
    10.        System.out.println("Father static Function");    
    11.    }    
    12.    
    13.    public void Function(){    
    14.        System.out.println("Father Function");    
    15.    }    
    16.}    
    


子類1

1.public class ChildOne extends Father {    
2.    static {    
3.        System.out.println("ChildOne static Create");    
4.    }    
5.    {    
6.        System.out.println("ChildOne Create");    
7.    }    
8.        
9.    public static void StaticFunction(){    
10.        System.out.println("ChildOne static Function");    
11.    }    
12.    
13.}  

 

子類2

1.public class ChildTwo extends Father {    
2.    static {    
3.        System.out.println("ChildTwo static Create");    
4.    }    
5.    {    
6.        System.out.println("ChildTwo Create");    
7.    }    
8.    
9.    public static void StaticFunction() {    
10.        System.out.println("ChildTwo static Function");    
11.    }    
12.    
13.    public void Function() {    
14.        System.out.println("ChildTwo Function");    
15.    }    
16.}   

 


測試類:

1.public class Main {    
2.    public static void main(String[] args) {    
3.        Father A = new ChildOne();    
4.        Father B = new ChildTwo();    
5.        A.StaticFunction();    
6.        A.Function();    
7.        B.StaticFunction();    
8.        B.Function();    
9.    }  
10.}  



最後的輸出結果請大家寫一下,不用去除錯,憑自己的理解寫寫試試

原文連結:http://www.iteye.com/topic/1115830

相關文章