//abc.java -- @CopyLeft by tsaiwn@csie.nctu.edu.tw //please compare to abc.cpp import static java.lang.System.*; class Dog { public Dog() { wet=38; printf("Wan wan! "); } // constructor protected int wet; // so that can be used in GueiDog static void printf(String f, Object... p){ out.printf(f, p); } }; // do NOT forget the ";" class GueiDog extends Dog { // Inheritance public GueiDog( ) { wet=49; printf("貴賓狗! "); } // constructor }; // do NOT forget the ";" class abc{ public static void main(String xx[ ]) { Dog x = new Dog( ); Dog x2; // note that Java has no pointer x.printf("x.wet = " + x.wet +"\n"); x.printf("\nHa ha ha 來個 GueiDog:\n"); GueiDog y = new GueiDog( ); x.printf("y.wet = " + y.wet + "\n"); y.printf( "------now do x2 = new Dog( ) \n" ); x2 = new Dog( ); // 這時才生出 Dog }//main( } // class /*************8 D:\COURSE\CS001\talk>javac abc.java D:\COURSE\CS001\talk>java abc Wan wan! x.wet = 38 Ha ha ha 來個 GueiDog: Wan wan! 貴賓狗! y.wet = 49 ------now do x2 = new Dog( ) Wan wan! D:\COURSE\CS001\talk> *****************************************************/ /******************************************************** C++ version D:\COURSE\OOP> g++ abc.cpp D:\COURSE\OOP> a.exe Wan wan! x.wet = 38 Ha ha ha 來個 GueiDog: Wan wan! 貴賓狗! y.wet = 49 ------ Wan wan! D:\COURSE\CS001\talk> *******************************************/