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-16  คลาส Animal, Dog, และ Basenji
public class Animal {
  public void makeSound() { }
  public static void makeSound(Animal[] a) {
    for(int i=0; i<a.length; i++) {
      a[i].makeSound();
    }
  }
  //...
public class Dog extends Animal {
  public void bark() {
    makeSound();
  }
  public void makeSound() {
    System.out.println("โฮ่ง ๆ");
  }
  //...
public class Cat extends Animal {
  public void meow() {
    makeSound();
  }
  public void makeSound() {
    System.out.println("เหมียว ๆ");
  }
  //...
public class Basenji extends Dog {
  public void makeSound() {
    System.out.println("...");
  }
  public void hunt(Animal a) {...}
  //...
©2009 S.Prasitjutrakul