`
jianchen
  • 浏览: 334096 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

java基础复习(15)-多态问题强化

阅读更多

多态题目:
题一:

 

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().

1]Please note that method1() of Parent class is private, because of which the same method (method1() of Parent class) will be invoked.

2] Had this method(method1() of Parent class) been public/protected/friendly (default), Child's class method1() would be called.

Thus C is correct answer.
由以上可知,父类方法如果被修饰为private,子类根本无法访问该方法,不具备可见性,所以你在子类中即使写了一个同样的方法(当然修饰级别要提高),也不构成覆盖。

在netbeans下,我测试了,如果你覆写了一个方法,编辑器会提示你加上@override注解,但你在子类中想覆写一个父类的private方法,却没有提示要加上该注解,也可以证明这根本没有构成覆写,所以要覆写的方法必须修饰级别不能为private。

所以猜想子类构成覆写的方法才能以多态的方法调用。

题二:

 

class Parent {
       public static int countA = 10;
       public  int countB = 20;
       public static void methodA() {
              System.out.println("methodA() in Parent " + countA);
       }

       public void methodB() {
              System.out.println("methodB() in Parent " + countA + countB);
       }
}

public class Child extends Parent {
       public static int countA = 30;

       public  int countB = 40;

       public static void methodA() {
              System.out.println("methodA() in Child  "+ countA );
       }
       public void methodB() {
              System.out.println("methodB() in Child  "+ countA + countB);
       }
       public static void main(String[] args) {
              Parent parent = new Child();
              parent.methodA();
              parent.methodB();
       }
}
 



KEY:
methodA() in Parent 10
methodB() in Child  3040
分析:
对于静态方法,也不构成多态。因为静态方法不依赖于类的实例,根据变量的类型,就可以找到对应的静态方法,无需考虑多态。

题目三:

 

class Base{
       int i = 99;
       public void amethod(){
              System.out.println("Base.amethod()");
           }
           Base(){
            amethod();
            }
       }
       public class Derived extends Base{
       int i = -1;
       public static void main(String argv[]){
            Base b = new Derived();
              System.out.println(b.i);
              b.amethod();
          }
          public void amethod(){
              System.out.println("Derived.amethod()");
          }
       }
 


 分析: In this situation a reference to any of the fields such as i will refer to the value in the Base class
直接访问父类的变量,不会构成多态,访问的永远是父类的成员变量,不过为了访问子类的成员变量可以使用方法来间接访问,这样就可以利用多态了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics