返回列表 發帖
本帖最後由 李穎俊 於 2021-2-6 10:56 編輯
  1. package aa;
  2. public class aa {
  3.     public static void main(String[] args) {
  4.             Dog d1=new Dog("憨憨",2,1.3f,"紅棕");
  5.             Dog d2=new Dog("球球",1,1.2f,"白");
  6.             d1.showProfile();
  7.             d1.makeSound(2);
  8.             d2.showProfile();
  9.             d2.makeSound(3);
  10.     }
  11. }

  12. class Dog{
  13.    
  14.     String name,color;
  15.     int age;
  16.     float w;
  17.    
  18.     Dog(String n,int a,float w,String c)
  19.     {
  20.             name=n;
  21.             age=a;
  22.             this.w=w;
  23.             color=c;
  24.     }
  25.     void showProfile()
  26.     {
  27.             System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+"色。");
  28.     }
  29.    
  30.     void makeSound(int n)
  31.     {
  32.             for(int i=1; i<=n; i++)
  33.                     System.out.print("汪~");
  34.             System.out.println();
  35.     }      
  36. }
複製代碼

TOP

返回列表