- public class Ch68 {
- public static void main(String[] args) {
- Dog d1=new Dog("Dog 1",2,1.28);
- Dog d2=new Dog("Dog 2",1,1.35);
- Cat c1=new Cat("Cat 1",3,0.95);
- d1.showProfile();
- d2.showProfile();
- c1.showProfile();
- }
- }
- 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+" this year is "+age+" years old, weight \"+w+"kg.");
- }
- }
- class Dog extends Animal
- {
- Dog(String n, int a, double w)
- {
- super(n,a,w);
- }
- }
- class Cat extends Animal
- {
- Cat(String n, int a, double w)
- {
- super(n,a,w);
- }
- }
複製代碼 |