9. สร้างใหม่จากเก่า

The Camtasia Studio video content presented here requires JavaScript to be enabled and the latest version of the Macromedia Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

 
รหัสที่ 9-17  ตัวอย่างการเรียก makeSound กับอ็อบเจกต์หลายประเภท
public class TestAnimal {
  public static void main(String[] args) {
    Cat     c1 = new Cat();
    Basenji b1 = new Basenji();  
    Dog     d1 = new Dog();
    Dog     d2 = new Basenji();
    Animal  a1 = new Dog();
    Animal  a2 = new Basenji();
    Animal  a3 = new Cat();
    c1.makeSound();
    b1.makeSound();
    d1.makeSound();
    d2.makeSound();
    a1.makeSound();
    a2.makeSound();
    a3.makeSound();
  }
}
©2009 S.Prasitjutrakul