java 调用构造器就建立对象吗?

直接上代码:java

class Father {
    private String aaa = "我来测试";
    public Father() {
        System.out.println("father this.getClass():"+this.getClass());//表示当前运行时的对象的class
        System.out.println("father this.hashCode(:"+this.hashCode());
        System.out.println("father this:"+this);
        System.out.println("father this.aaa:"+this.aaa);
        this.test();
    }
    private void test(){
        System.out.println("我是私有化的方法");
    }

    public int dowork() {
        System.out.println("我要去工做了");
        return 1;
    }
}

class Son extends Father {
    private String bbb = "我来测试1";
    public Son() {
        System.out.println("son super.getClass():"+super.getClass());//表示父类运行时的class
        System.out.println("son this.hashCode():"+this.hashCode());
        System.out.println("son this:"+this);
    }
}

public class FatherDemo{
    public static void main(String[] args) {
        Father f = new Son();
    }
}

运行结果是这样的:ide

father this.getClass():class xiaomage.Son
father this.hashCode(:581882789
father this:xiaomage.Son@22aed3a5
father this.aaa:我来测试
我是私有化的方法
son super.getClass():class xiaomage.Son
son this.hashCode():581882789
son this:xiaomage.Son@22aed3a5

测试

相关文章
相关标签/搜索