close

(1) 此程式中有一列 super(999);
      第一個做出含該列以及不含該列之 running script
      並且正確說明有何不同的可以賺到期中考3分
  (2) 此程式改自以前給大家的man.C 之C++程式(Animal, Mankind 繼承範例)
      試著與之對照看看, (man.C 在/net/ftp/pub/CSIE/course/cs2/man.C )
      第一個正確列出C++和Java在這範例看出哪些不同規定的可以賺到期中考3分
  follow up 時注意你的引言形式

   1 //to test constructor / inheritance,    CopyLeft by tsaiwn@csie.nctu.edu.tw
   2 // this testM.java will generate three .class files; 也可拆成三個.java程式檔
   3 class Animal {
   4      int pv1;  //注意沒寫屬性並非"private", 是package available,此與C++不同
   5     private double pv2;
   6     protected int pt1[]={0,1,2,3,4};
   7     public float pb1;
   8     public int pb2[]={10, 11,12,13,14,15,16};
   9     public Animal(){ System.out.println("Animal pop out!");};
  10     public Animal(String x){ System.out.println("Animal String "+x);};
  11     public Animal(int x){
  12    pv1=x;  pv2=45.67;
  13    this.pb1=135.246f;
  14            System.out.println("Animal shows up");
  15     };
  16     void talk() {
  17            System.out.println("Animal talk, pv1="+pv1);
  18            System.out.println("\t pv2="+ pv2);
  19            System.out.println("\t pb1="+ pb1);
  20            System.out.println("\t pb2[6]="+ pb2[6]);
  21     };
  22 };
  23 class Mankind extends Animal{
  24       String pv3;
  25     public Mankind(String name) {  // Constructor
  26         super(999);  // TRY to COMMENT OUT this line and Test again
  27         pv3=name;    // .. 注意若沒寫super(999); 等於寫 super();
  28         System.out.println("Mankind "+ pv3 + " appears");
  29     }
  30     public int pb3[]={30,31,32,33,34,35,36,37,38};
  31     public void talk() {
  32        System.out.print("Mankind talk, pb2[0]=" + pb2[0]);
  33        System.out.println(", pv3=" + pv3 );
  34        System.out.println("Mankind talk, pt1[3]=" + pt1[3]);
  35        System.out.println("Mankind talk, pb3[3]=" + pb3[3]);
  36        System.out.println("              pv1="+pv1);
  37        // System.out.println("              pv2="+pv2); //error, 因private
  38     };
  39 }  // C++ 需要用 ";" 結束class, 但 Java 不必了
  40 public class testM{  // filename 必須與此public class 同名: testM.java
  41      static Animal a, aa1=new Animal(123), aa2= new Animal(456);
  42      static Mankind  mm1, mm2;  // object reference only, no object yet
  43    public static void main(String s[]){  //別忘了static且參數是String array
  44       System.out.println("Welcome to taste Java ---first let aa1 talk:");
  45       aa1.talk();
  46       mm1= new Mankind("Chang-3");
  47       mm2= new Mankind("Lee-4");
  48       System.out.println("mm1.pb1=" + mm1.pb1);
  49       System.out.print(" (Let mm1 talk)\n");     mm1.talk();
  50       System.out.print(" (and then DO  a=mm2; a.talk();)\n");
  51       a = mm2;    // 這是可以的, 因為 a 屬於 mm2 所屬的父類別(super class)
  52       a.talk();  // 仍會用 Mankind 裡的 talk(); 因Polymorphism 多型!
  53       System.out.print(" (and then DO  mm2=(Mankind)aa1; mm2.talk();)\n");
  54       mm2 = (Mankind) aa1;   // "cast" is required! 但仍會Runtime Exception
  55       //mm2.talk();
  56    }
  57 }   //(別告訴我說你到現在還不會將此篇存檔然後去掉line numbers)

arrow
arrow
    文章標籤
    Java繼承
    全站熱搜

    英國旅遊民宿推薦 發表在 痞客邦 留言(0) 人氣()