// am9.java --- by tsaiwn@csie.nctu.edu.tw // overloading the Mankind.talk( ) function import java.awt.*; public class am9 extends MyApplet { public static void main(String[ ] args) { Animal a = new Animal(1); Mankind m = new Mankind(3); m.init( ); Frame f = new Frame("Zoo"); print("Animal talking..."); a.talk( ); println(""); Cat cat = new Cat(2); cat.init( ); // then, put cat into the Frame f.add(cat); f.setSize(300,300); f.validate( ); f.setVisible(true); print("Cat talking..."); cat.talk( ); try { Thread.sleep(568); } catch (Exception e) {} println(""); f.remove(cat); f.add(m); f.setSize(300,300); f.validate( ); f.setVisible(true); print("Then, Mankind m talk:"); m.talk("speech.au"); println(""); try { Thread.sleep(8588); } catch (Exception e) {} //newPlayFile("bye.au"); System.exit(0); } // main } // am9 class Animal extends MyApplet { // Animal is-a MyApplet (is-a Applet) protected int n = 0; public Animal(int n) { this.n = n; } // void talk( ) { for(int i=1; i <= n; ++i) { beep(); newPlayFile("chichi.au"); } } } // class Animal class Cat extends Animal { public Cat(int n) { super(n); } // public void init ( ) { PicturePanel p = new PicturePanel("images/cat.gif", true); setLayout( new GridLayout(1,2)); add(p); Panel p23 = new Panel(new GridLayout(2,1)); PicturePanel p2a = new PicturePanel("images/cat1.gif", false); PicturePanel p2b = new PicturePanel("images/cat2.gif", false); p23.add(p2a); p23.add(p2b); add(p23); } // new talk( ) for Cat, 若不寫, 則會用原先 Animal 的 talk void talk( ) { for(int i=1; i <= n; ++i) newPlayFile("meow.wav"); } } // Cat class Mankind extends Animal { public Mankind(int n) { super(n); } public void init ( ) { PicturePanel p = new PicturePanel("images/mypic.jpg"); setLayout( new GridLayout(1,1)); add(p); } // new talk( ) for Mankind, 若不寫, 則會用原先 Animal 的 talk void talk( ) { for(int i=1; i <= n; ++i) { newPlayFile("yahoo.au"); try { Thread.sleep(123); } catch (Exception e) {} } } // talk() void talk(String what) { newPlayFile(what); } // talk(String) } // class Mankind