子类对象实例化全过程

子类对象实例化全过程:

package com.aff.sup;

public class TestDog {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.setAge(2);
        d.setName("天天");
        d.setHostName("芳芳");
        System.out.println("name:" + d.getName() + "age:" + d.getAge() + "hostName:" + d.getHostName());
        System.out.println(d.toString());
    }

}

// 生物
class Creator {
    private int age;

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

    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("this is  Animal's constructor");
    }

    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("this is  Dog's constructor");
    }

    public String getHostName() {
        return hostName;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

}

输出结果:

this is Creator's constructor
this is Animal's constructor
this is Dog's constructor
name:天天age:2hostName:芳芳
com.aff.sup.Dog@6d06d69c

All that work will definitely pay off
原文地址:https://www.cnblogs.com/afangfang/p/12516848.html