- class Ch73
- {
- public static void main(String args[])
- {
- Dog d1=new Dog("5643545345",-12342132,2e-10),d2=new Dog("A",200,2.434e-30);
- d1.showProfile(); d1.makeSound(1);
- d2.showProfile(); d2.makeSound(10);
- Cat c=new Cat("€",-2,0.000000000001);
- c.showProfile(); c.makeSound(2000) ;
- }
- }
- class Animal
- {
- String name;
- int age;
- double w;
- Animal(String n,int a,double w)
- {
- name=n;
- age=a;
- this.w=w;
- }
- void showProfile()
- {
- System.out.println(name+"今年"+age+"歲,體重"+w+"公斤");
- }
- }
- class Dog extends Animal
- {
- Dog(String n,int a,double w)
- {
- super(n, a, w) ;
- }
- void makeSound(int x)
- {
- for(int i=0;i<x;i++)
- {
- System.out.print("汪~");
- }
- System.out.println();
- }
- }
- class Cat extends Animal
- {
- Cat(String n,int a,double w )
- {
- super(n, a, w) ;
- }
- void makeSound(int x)
- {
- for(int i=0;i<x;i++)
- {
- System.out.print("喵~");
- }
- System.out.println();
- }
- }
複製代碼 |