<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我思故我強

    第六部分:覆蓋,重載,運行時期類型及其面向對象

    第六部分:覆蓋,重載,運行時期類型及其面向對象

    • 知道面向對象設計中封裝的好處以及如何實現,能知道is a和has a的意義。
    • 能正確使用覆蓋和重載的方法,能正確調用父類或重載的構造方法(constructor),知道調用這些方法后的結果。
    • 能正確實例化類或內部類

    §1.1.1      

    3. What will happen when you attempt to compile and run the following code?

    class MyParent

    {

    int x, y;

    MyParent(int x, int y)

    {

    this.x = x;

    this.y = y;

    }

    public int addMe(int x, int y)

    {

    return this.x + x + y + this.y;

    }

    public int addMe(MyParent myPar)

    {

    return addMe(myPar.x, myPar.y);

    }

    }

    class MyChild extends MyParent

    {

    int z;

    MyChild (int x, int y, int z)

    {

    super(x,y);

    this.z = z;

    }

    public int addMe(int x, int y, int z)

    {

    return this.x + x + this.y + y + this.z + z;

    }

    public int addMe(MyChild myChi)

    {

    return addMe(myChi.x, myChi.y, myChi.z);

    }

    public int addMe(int x, int y)

    {

    return this.x + x + this.y + y;

    }

    }

    public class MySomeOne

    {

    public static void main(String args[])

    {

    MyChild myChi = new MyChild(10, 20, 30);

    MyParent myPar = new MyParent(10, 20);

    int x = myChi.addMe(10, 20, 30);

    int y = myChi.addMe(myChi);

    int z = myPar.addMe(myPar);

    System.out.println(x + y + z);

    }

    }

     

    Choices:

    a. 300

    b. 240

    c. 120

    d. 180

    e. Compilation error

    f. None of the above

    ――――――――――――

    A is the correct choice. In the code, MyChild class overrides the addMe(int x, int y) method of the MyParent class. And in both the MyChild and MyParent class, addMe() method is overloaded. There is no compilation error anywhere in the above code. On execution, first, the object of MyChild class will be constructed. Please note that there is a super() call from the constructor of MyChild class, which will call the constructor of MyParent class. This will cause the value of z variable of MyChild class to be 30 and x, y variables of MyParent class will become 10 and 20 respectively. The next statement will again call the constructor of MyParent class with same x and y values. This is followed by execution of addMe() method of MyChild class with x as 10, y as 20 and z as 30. Also x and y are inherited by MyChild class from the MyParent class. Thus in the addMe() method of the MyChild class, the value of this.x will be 10, this.y will be 20 and this.z will be 30. The return value of this method will be "10 + 10 + 20 + 20 + 30 + 30", which is equal to 120. Thus x will become 120. This is followed by the invocation of the other addMe() method which takes object reference of the MyChild class. From this method, the method which was called earlier is invoked. This call is exactly the same as the earlier one. Thus the value of y will also be 120 like x. Now the addMe() method of MyParent class is invoked. This method invokes another addMe() method of the same class. Its equivalent to the invocation of addMe(int x, int y) method with x as 10 and y as 20. Also the value of instance variables x and y of My Parent class is 10 and 20 respectively. The value of z will be evaluated to "10 + 10 + 20 + 20", which is equal to 60. Thus the value of x, y and z after all the invocations will be 120, 120 and 60 respectively. As a result of this finally, "120 + 120 + 60" which is equal to 300 will be printed. Thus A is the correct choice.

     

    §1.1.2      

    Given the code below, and making no other changes, which access modifiers

    (public, protected or private) can legally be placed before myMethod() on line 3?

    If line 3 is left as it is, which keywords can legally be placed before myMethod

    on line 8?

    1.class HumptyDumpty

    2.{

    3.void myMethod() {}

    4.}

    5.

    6.class HankyPanky extends HumptyDumpty

    7.{

    8.void myMethod() {}

    9.}

    Choices:

    a. private or nothing(i.e. leaving it as it is) on line 3.

    Nothing(i.e. leaving it as it is) or protected or public

    on line 8.

    b. public or protected on line 3. private or nothing(i.e. leaving it

    as it is) on line 8.

    c. nothing(i.e. leaving it as it is) or protected or public on

    line 3. private or nothing(i.e. leaving it as it is) on line 8.

    d. None of the above.

    A is correct. The basic principle is that a method cannot be overridden to be more private. Since the method is being overridden to be friendly(default modifier) it can only be private or friendly in the superclass. Secondly if the method in superclass is left as it is(i.e. friendly access) the method in subclass can be friendly, protected or public.

    §1.1.3      

    What results from the following code?

    1.class MyClass

    2.{

    3.void myMethod(int i) {System.out.println("int version");}

    4.void myMethod(String s) {System.out.println("String version");}

    5.public static void main(String args[])

    6.{

    7.MyClass obj = new MyClass();

    8.char ch = 'c';

    9.obj.myMethod(ch);

    10.}

    11.}

    Choices:

    a. Line 4 will not compile as void methods can't be overridden.

    b. An exception at line 9.

    c. Line 9 will not compile as there is no version of myMethod which takes a char as argument.

    d. The code compiles and produces output: int version.

    e. The code compiles and produces output: String version.

    ――――――――――

    D is correct. A is incorrect as void methods can be overridden without any problem. B is incorrect as char ch declaration is valid. C is incorrect as char type in java is internally stored as integer and there is a method which takes int as an input. D is correct, on line 9 char ch is widened to an int and passed to int version of the myMethod(). E is incorrect as int version of myMethod() is called.

    §1.1.4      

    What is displayed when the following is executed?

    class Parent

    {

    private void method1()

    {

    System.out.println("Parent's method1()");

    }

    public void method2()

    {

    System.out.println("Parent's method2()");

    method1();

    }

    }

    class Child extends Parent

    {

    public void method1()

    {

    System.out.println("Child's method1()");

    }

    public static void main(String args[])

    {

    Parent p = new Child();

    p.method2();

    }

    }

    Choices:

    a. Compile time error

    b. Run time error

    c. prints : Parent's method2()

              Parent's method1()

    d. prints : Parent's method2()

              Child's method1()

    ―――――――

    C is correct. The code will compile without any error and also will not give any run time error. The variable p refers to the Child class object. The statement p.method2() on execution will first look for method2() in Child class. Since there is no method2() in child class, the method2() of Parent class will be invoked and thus "Parent's method2()" will be printed. Now from the method2() , there is a call to method1(). Please note that method1() of Parent class is private, because of which the same method (method1() of Parent class) will be invoked. Had this method(method1() of Parent class) been public/protected/friendly (default), Child's class method1() would be called. Thus C is correct answer.

     

    §1.1.5      

    What is displayed when the following is executed?

    class Parent{

    private void method1(){

    System.out.println("Parent's method1()");

    }

    public void method2(){

    System.out.println("Parent's method2()");

    method1();

    }

    }

    class Child extends Parent{

    public void method1(){

    System.out.println("Child's method1()");

    }

    public static void main(String args[]){

    Parent p = new Child();

    p.method2();

    }

    }

     

    A. compile time error

    B. run time error

    C. prints: parent’s method2()  parent’s method1()

    D. prints: parent’s method2()  child’s method1()

     

    C is correct. The code will compile without any error and also will not give any run time error. The variable p refers to the Child class object. The statement p.method2() on execution will first look for method2() in Child class. Since there is no method2() in child class, the method2() of Parent class will be invoked and thus "Parent's method2()" will be printed. Now from the method2() , there is a call to method1(). Please note that method1() of Parent class is private, because of which the same method (method1() of Parent class) will be invoked. Had this method(method1() of Parent class) been public/protected/friendly (default), Child's class method1() would be called. Thus C is correct answer.

     

    §1.1.6      

      1) class Person {

     

      2) public void printValue(int i, int j) { }

     

      3) public void printValue(int i){ }

     

      4) }

     

      5) public class Teacher extends Person {

     

      6) public void printValue() { }

     

      7) public void printValue(int i) {}

     

      8) public static void main(String args[]){

     

      9) Person t = new Teacher();

     

      10) t.printValue(10);

     

      11) }

     

      12) }

     

      Which method will the statement on line 10 call?

      A. on line 2

     

      B. on line 3

     

      C. on line 6

     

      D. on line 7

      翻譯

      第十行的聲明將調用哪些方法。

     

      答案 D

     

      解析 變量t是一個Person對象,但是它是用Teacher實例化的,這個問題涉及到java的

    編譯時多態和運行時多態的問題,就編譯時多態來說,t實際上是一個Person類,這涉及到類

    型的自動轉換(將一個子類的實例賦值給一個父類的變量是不用進行強制類型轉換,反之則需

    要進行強制類型轉換,而且被賦值的變量實際上應該是一個子類的對象),如果對t調用了子

    類中新增的方法則造成編譯時錯誤編譯將不能通過,而在運行時,運行時系統將根據t實際指

    向的類型調用對應的方法,對于本例來說,t.print(10)將調用t實際指向的Teacher類的對應

    方法。在java中,可以用一個子類的實例實例化父類的一個變量,而變量在編譯時是一個父類

    實例,在運行時可能是一個子類實例。

     

    §1.1.7      

    35、public class Parent {

      public int addValue( int a, int b) {

      int s;

      s = a+b;

      return s;

      }

      }

      class Child extends Parent {

     

      }

      Which methods can be added into class Child?

      A. int addValue( int a, int b ){// do something...}

     

      B. public void addValue (){// do something...}

     

      C. public int addValue( int a ){// do something...}

     

      D. public int addValue( int a, int b )throws MyException {//do

    something...}

     

      (bc)

     

      題目:哪些方法可以加入類Child中。

     

      此題涉及方法重載(overload),方法重寫(override)以及類派生時方法重寫的規則

    。方法重載的規則是:一、參數列表必須不同,個數的不同完全可以,如果個數相同則參數類

    型的不同不能引起歧意,例如int

    和long,float和double就不能作為唯一的類型不同;二、返回值可以不同,但是不能是重載

    時唯一的不同點(這點和c++中不同,c++中返回類型必須一致)。方法重寫發生在類繼承時,

    子類可以重寫一個父類中已有的方法,必須在返回類型和參數列表一樣時才能說是重寫,否則

    就是重載,java中方法重寫的一個重要而且容易被忽略的規則是重寫的方法的訪問權限不能比

    被重寫的方法的訪問權限低!重寫的另一個規則是重寫的方法不能比被重寫的方法拋棄(thro

    ws)更多種類的異常,其拋棄的異常只能少,或者是其子類,不能以拋棄異常的個數來判斷種

    類,而應該是異常類層次結果上的種類。此題中答案a的錯誤就是重寫的訪問權限比被重寫的

    方法的低,而b,c都屬于重載,d的錯誤在于比被重寫的方法拋棄了更多種類的異常。

    posted on 2009-10-16 11:42 李云澤 閱讀(274) 評論(0)  編輯  收藏 所屬分類: 面試筆試相關的SCJP認證學習

    主站蜘蛛池模板: 亚洲综合色区在线观看| 国产在线观看麻豆91精品免费| 青青草国产免费久久久下载| 亚洲精品视频专区| 午夜免费啪视频在线观看| 亚洲AV中文无码乱人伦下载| 中文字幕久无码免费久久| 亚洲线精品一区二区三区影音先锋 | 国产a不卡片精品免费观看| 亚洲精品无码久久久久牙蜜区| 一本岛高清v不卡免费一三区| 亚洲国语在线视频手机在线| 在线看片v免费观看视频777 | 亚洲欧洲一区二区三区| 精品97国产免费人成视频| 国产专区一va亚洲v天堂| 久久久精品国产亚洲成人满18免费网站 | 中文亚洲AV片在线观看不卡| 中国在线观看免费的www| 亚洲国产一区二区三区青草影视| 最近2019免费中文字幕视频三| 亚洲精品91在线| 妞干网手机免费视频| 美女羞羞视频免费网站| 伊人久久综在合线亚洲91| 久久免费视频观看| 亚洲制服丝袜中文字幕| 国产一精品一aⅴ一免费| 成在线人免费无码高潮喷水| 亚洲国产精品热久久| 毛片a级毛片免费观看品善网| 男性gay黄免费网站| 亚洲精品国偷自产在线| 国产精彩免费视频| 国产AV日韩A∨亚洲AV电影| 亚洲国产精品乱码一区二区| 91在线视频免费播放| 未满十八私人高清免费影院| 亚洲AV永久无码精品| 免费的一级黄色片| 99免费精品视频|