首页 > 代码库 > JAVA学习--子类对象实例化的全过程

JAVA学习--子类对象实例化的全过程

public class TestDog {
 

   publicstatic void main(String[] args) {
       Dog d = newDog();
      d.setAge(10);
      d.setName("花花");
      d.setHostName("小明");

      System.out.println("name:" + d.getName() + " age:" +d.getAge()
             +"hostName:" + d.getHostName());
      
      System.out.println(d.toString());
    }
}

// 生物
class Creator {
    private intage;

    public intgetAge() {
       returnage;
    }

    public voidsetAge(int age) {
       this.age =age;
    }

    publicCreator() {
      super();
      System.out.println("this is Creator‘s constructor");
    }

}

// 动物类
class Animal extends Creator {
    privateString name;

    publicString getName() {
       returnname;
    }

    public voidsetName(String name) {
       this.name =name;
    }

    publicAnimal() {
      super();
      System.out.println("this is Animal‘s constructor");
    }

}

// 狗
class Dog extends Animal {
    privateString hostName;

    publicString getHostName() {
       returnhostName;
    }

    public voidsetHostName(String hostName) {
      this.hostName = hostName;
    }

    public Dog(){
      super();
      System.out.println("this is Dog‘s constructor");
    }

}

JAVA学习--子类对象实例化的全过程