直接上代碼: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
測試