代码:
//生物 class Creator { private int age; public Creator() { super(); System.out.println("这是生物的构造器"); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } //动物类 class Animal extends Creator { private String name; public Animal() { super(); System.out.println("这是动物的构造器"); } public String getName() { return name; } public void setName(String name) { this.name = name; } } //狗 class Dog extends Animal { private String hostName; public Dog() { super(); System.out.println("这是狗的构造器"); } public String getHostName() { return hostName; } public void setHostName(String hostName) { this.hostName = hostName; } } public class TestDog { public static void main(String[] args) { Dog dog = new Dog(); dog.setAge(10); dog.setName("花花"); dog.setHostName("小明"); System.out.println(dog.getName() + dog.getAge() + dog.getHostName()); } }
结果:
这是生物的构造器
这是动物的构造器
这是狗的构造器
花花10小明
内存图: