返回列表 發帖
  1. public class Ch06
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 Dog d1=new Dog("憨憨",2,1.3,"紅棕");
  6.                 Dog d2=new Dog("球球",1,1.2,"白");
  7.                 d1.showProfile();
  8.                 d1.makeSound(2);
  9.                 d2.showProfile();
  10.                 d2.makeSound(3);  
  11.                 Cat c1=new Cat("咪咪",3,1.5,"銀灰");
  12.                 c1.showProfile();
  13.                 c1.makeSound(5);
  14.                 System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓.");
  15.         }
  16. }

  17. class Dog
  18. {
  19.         static int sum=0;
  20.     int age,count;
  21.     double weight;
  22.     String color,name;
  23.     Dog(String name,int age,double weight,String color)
  24.     {
  25.                 sum++;
  26.             this.age=age;
  27.             this.weight=weight;
  28.             this.color=color;
  29.             this.name=name;
  30.     }
  31.     void makeSound(int count)
  32.     {
  33.             for(int i=1;i<=count;i++)
  34.             {
  35.                     System.out.print("汪~");
  36.             }
  37.             System.out.println();
  38.     }
  39.     void showProfile()
  40.     {
  41.             System.out.println(name+"今年"+age+"歲, 體重"+weight+"公斤,毛色為"+color+"色.");
  42.     }
  43. }

  44. class Cat
  45. {
  46.         static int sum=0;
  47.     int age,count
  48.     ;
  49.     double weight;
  50.         String color,name;
  51.         Cat(String name,int age,double weight,String color)
  52.         {
  53.                     sum++;
  54.                 this.age=age;
  55.                 this.weight=weight;
  56.                 this.color=color;
  57.                 this.name=name;
  58.         }
  59.         void makeSound(int count)
  60.         {
  61.                 for(int i=1;i<=count;i++)
  62.                 {
  63.                         System.out.print("喵~");
  64.                 }
  65.         }
  66.         void showProfile()
  67.         {
  68.                 System.out.println(name+"今年"+age+"歲, 體重"+weight+"公斤,毛色為"+color+"色.");
  69.         }
  70. }
複製代碼

TOP

返回列表