返回列表 發帖
本帖最後由 李彣 於 2023-8-23 21:15 編輯
  1. public class C
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 Dog d1=new Dog("dog1",2,1.2f,"黑");
  6.         Dog d2=new Dog("dog2",1,1.1f,"白");
  7.         d1.showProfile();
  8.         d1.makeSound(1);
  9.         d2.showProfile();
  10.         d2.makeSound(2);
  11.         }
  12. }
  13. class Dog
  14. {
  15.    
  16.     String name,color;
  17.     int age;
  18.     float w;
  19.    
  20.     Dog(String n,int a,float w,String c)
  21.     {
  22.             this.name=n;
  23.         this.age=a;
  24.         this.w=w;
  25.         this.color=c;
  26.     }
  27.     void showProfile()
  28.     {
  29.             System.out.println(this.name+"今年"+this.age+"歲,體重"+this.w+"公斤,毛色為"+this.color+"色。");
  30.     }
  31.     void makeSound(int n)
  32.     {
  33.             for(int i=1; i<=n; i++)
  34.             {
  35.                     System.out.print("汪~");
  36.                     System.out.println();
  37.             }
  38.     }
  39. }
複製代碼

TOP

返回列表