继承行为

时间:2016-02-19 16:30:00

标签: java inheritance polymorphism

class A {

  public void print(A a) {
    System.out.println("A");
    a.add();
  }

  public void add() {
    System.out.println("At A.add()");
  }
}

class B extends A {

  public void print(B b) {
    System.out.println("B");
    b.add();
  }

  public void add() {
    System.out.println("At B.add()");
  }
}

public class MDemo {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    A a = new B();
    a.print(a);
    ;
  }
}

输出

A
At B.add()

请澄清输出。 参考来自 Selecting overriding method java with multiple options

0 个答案:

没有答案