- public class Ch30 {
- public static void main(String[] args) {
- Dog aa=new Dog("和漢",3,1.5,"棕");
- aa.showP();
- aa.makesound(5);
- Dog ab=new Dog("球球",5,2.3,"白");
- ab.showP();
- ab.makesound(3);
- Cat b=new Cat("喵貓",1,0.8,"白");
- b.showP();
- b.makesound(5);
- System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓");
- }
- }
- class Dog
- {
- static int sum=0;
- int age;
- double sou,g;
- String name,col;
- Dog(String a,int x,double y,String q)
- {
- sum++;
- name =a;
- col=q;
- age=x;
- g=y;
- }
- void showP()
- {
- System.out.println(name+"今年"+age+"歲,體重"+g+"公斤,毛色為"+col+"色");
- }
- void makesound(int n)
- {
- for(int i=1; i<=n; i++)
- System.out.print("旺~");
- System.out.println();
- }
- }
- class Cat
- {
- static int sum=0;
- int age;
- double sou,g;
- String name,col;
- Cat(String a,int x,double y,String q)
- {
- sum++;
- name =a;
- col=q;
- age=x;
- g=y;
- }
- void showP()
- {
- System.out.println(name+"今年"+age+"歲,體重"+g+"公斤,毛色為"+col+"色");
- }
- void makesound(int n)
- {
- for(int i=1; i<=n; i++)
- System.out.print("喵~");
- System.out.println();
- }
- }
複製代碼 |