返回列表 發帖
  1. package test;
  2. public class Test {
  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.     String name,color;
  14.     int age;
  15.     float w;      
  16.     Dog(String n,int a,float w,String c)
  17.     {
  18.             this.name=n;
  19.             this.age=a;
  20.             this.w=w;
  21.             this.color=c;
  22.     }
  23.     void showProfile()
  24.     {
  25.             System.out.println(this.name+"今年"+this.age+"歲,體重"+this.w+"公斤,毛色為"+this.color+"色。");
  26.     }
  27.     void makeSound(int n)
  28.     {
  29.             for(int i=1; i<=n; i++)
  30.                     System.out.print("汪~");
  31.             System.out.println();
  32.     }      
  33. }
複製代碼

TOP

返回列表