返回列表 發帖
  1. public class ch63
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         Dog d1=new Dog("憨憨",2,3.8f);
  6.         Dog d2=new Dog("球球",1,2.5f);
  7.         Cat c1=new Cat("咪咪",5,3.2f);
  8.         d1.showProfile();
  9.         d2.showProfile();
  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()
  21.     {
  22.     }
  23.     Dog(String name,int age,float w)
  24.     {
  25.         sum++;
  26.         this.name=name;
  27.         this.age=age;
  28.         this.w=w;
  29.     }
  30.     void showProfile()
  31.     {
  32.         System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  33.     }
  34. }   
  35. class Cat
  36. {
  37.     static int sum=0;
  38.     String name;
  39.     int age;
  40.     float w;
  41.     Cat()
  42.     {
  43.     }
  44.     Cat(String name,int age,float w)
  45.     {
  46.         sum++;
  47.         this.name=name;
  48.         this.age=age;
  49.         this.w=w;
  50.     }
  51.     void showProfile()
  52.     {
  53.         System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  54.     }
  55. }
複製代碼

TOP

返回列表