返回列表 發帖
  1. public class Ch04
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         Dog d1=new Dog("憨憨",2,3.8f);
  6.         d1.showProfile();
  7.         Dog d2=new Dog("球球",1,2.5f);
  8.         d2.showProfile();
  9.         Cat c1=new Cat("咪咪",3,3.2f);
  10.         c1.showProfile();
  11.         System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓.");
  12.     }
  13. }
  14. class Dog
  15. {
  16.         static int sum=0;
  17.     String name;
  18.     int age;
  19.     float w;
  20.     Dog(String n,int a,float w)
  21.     {
  22.         sum++;
  23.         name=n;
  24.         age=a;
  25.         this.w=w;
  26.     }
  27.     void showProfile()
  28.     {
  29.         System.out.println(this.name+"今年"+this.age+"歲,體重"+this.w+"公斤.");
  30.     }
  31.    
  32. }
  33. class Cat
  34. {
  35.     static int sum=0;
  36.     String name;
  37.     int age;
  38.     float w;
  39.     Cat(String n,int a,float w)
  40.     {
  41.         sum++;
  42.         name=n;
  43.         age=a;
  44.         this.w=w;
  45.     }
  46.     void showProfile()
  47.     {
  48.         System.out.println(this.name+"今年"+this.age+"歲,體重"+this.w+"公斤.");
  49.     }
  50. }
複製代碼

TOP

返回列表