返回列表 發帖
  1. public class Ch01
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 Dog d1=new Dog("憨憨",2,1.3,"紅棕");
  6.                 Dog d2=new Dog("球球",1,1.3,"白");
  7.                 Cat c1=new Cat("咪咪",3,1.5,"銀灰");
  8.                 d1.showProfile();
  9.                 d1.makeSound(2);
  10.                 d2.showProfile();
  11.                 d2.makeSound(3);
  12.                 c1.shoProfile();
  13.                 c1.makSound(3);
  14.                 System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓.");
  15.                
  16.         }
  17.         
  18. }
  19. class Dog
  20. {
  21.         static int sum;
  22.     int age,count;
  23.     double weight;
  24.     String color,name;
  25.    
  26.    
  27.     Dog(String name,int age,double weight,String color)
  28.     {
  29.                 sum++;
  30.             this.age=age;
  31.             this.weight=weight;
  32.             this.color=color;
  33.             this.name=name;
  34.     }
  35.    
  36.     void makeSound(int count)
  37.     {
  38.             for(int i=1;i<=count;i++)
  39.             {
  40.                     System.out.print("汪~");
  41.             }
  42.     }
  43.    
  44.    
  45.    
  46.    
  47.     void showProfile()
  48.     {
  49.             System.out.println(name+"今年"+age+"歲, 體重"+weight+"公斤,毛色為"+color+"色.");
  50.     }
  51.    
  52.    
  53.    
  54.    
  55. }
  56. class Cat
  57. {
  58.         static int sum;
  59.         int age1,count1;
  60.     double weight1;
  61.     String color1,name1;
  62.         Cat(String name1,int age1,double weight1,String color1)
  63.     {
  64.                 sum++;
  65.             this.age1=age1;
  66.             this.weight1=weight1;
  67.             this.color1=color1;
  68.             this.name1=name1;
  69.     }
  70.         void shoProfile()
  71.     {
  72.             System.out.println(name1+"今年"+age1+"歲, 體重"+weight1+"公斤,毛色為"+color1+"色.");
  73.     }
  74.         void makSound(int count1)
  75.     {
  76.             for(int i=1;i<=count1;i++)
  77.             {
  78.                     System.out.print("喵~");
  79.             }
  80.     }
  81. }
複製代碼

TOP

返回列表