返回列表 發帖
  1. public class Ch01
  2. {
  3.       
  4.         public static void main(String[] args) {
  5.                 Dog d1=new Dog("憨憨",2,3.8f);
  6.                 Dog d2=new Dog("球球",1,2.5f);
  7.                 d1.showProfile();              
  8.                 d2.showProfile();
  9.                 Cat c1=new Cat("咪咪",5,3.2f);
  10.                 c1.showProfile();
  11.                 System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓.");
  12.         }
  13. }

  14. class Dog{
  15.                 static int sum=0;
  16.         String name;
  17.         int age;
  18.         float w;
  19.       
  20.         Dog(String n,int a,float w)
  21.         {
  22.                 sum++;
  23.                         this.name=n;
  24.                 this.age=a;
  25.                 this.w=w;
  26.         }
  27.         void showProfile()
  28.         {
  29.                 System.out.println(this.name+"今年"+this.age+"歲,體重"+this.w+"公斤.");
  30.         }      
  31. }
  32. class Cat{
  33.    static int sum=0;
  34.     String name;
  35.     int age;
  36.     float w;
  37.    
  38.     Cat(String n,int a,float w)
  39.     {
  40.                     sum++;
  41.             this.name=n;
  42.             this.age=a;
  43.             this.w=w;
  44.     }
  45.     void showProfile()
  46.     {
  47.             System.out.println(this.name+"今年"+this.age+"歲,體重"+this.w+"公斤.");
  48.     }      

  49. }
複製代碼

TOP

返回列表