本帖最後由 洪振庭 於 2016-7-8 21:02 編輯
- public class Ch58{
- public static void main(String args[])
- {
- Dog d1=new Dog("紅棕色","球球",2,2.1);
- Dog d2=new Dog("白色","憨憨",9,3.5);
- d1.showProfile();
- d2.makeSound(5);
- d1.showProfile();
- d2.makeSound(16);
- }
- }
- class Dog
- {
- String color;
- String name;
- int age;
- double w;
- Dog(String c,String n,int a,double w)
- {
- color=c;
- name=n;
- age=a;
- this.w=w;
- }
- void makeSound(int n)
- {
- for(int i=1; i<=n; i++)
- System.out.print("汪~");
- System.out.println();
- }
- void showProfile()
- {
- System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
- }
- }
複製代碼 |