jackyrong

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

对初学者来说,两个比较好的例子,写出JAVA下的输出结果
1)
public class Test {
public static void main(String[] args) {
Child child = new Child();
}
}

class Parent {
Parent() {
System.out.println("to construct Parent.");
}
}

class Child extends Parent {
Child() {
System.out.println("to construct Child.");
}

Delegatee delegatee = new Delegatee();
}

class Delegatee {
Delegatee() {
System.out.println("to construct Delegatee.");
}
}

2)
class Base {
 Base() {
  System.out.println("Base() before print()");
  print();
  System.out.println("Base() after print()");
 }
 public void print() {
  System.out.println("Base.print()");
 }
}
class Derived extends Base {
 int value;
 Derived(int val) {
  value = val;
  System.out.println("Derived() with " + value);
 }
 public void print() {
  System.out.println("Derived.print() with " + value);
 }
}
public class Polymorphism {
 public static void main(String[] args) {
  new Derived(123);
 }
}

posted on 2005-11-02 09:58  jackyrong的世界  阅读(486)  评论(0编辑  收藏  举报