首页 > 其他 > 详细

this小案例

时间:2016-06-08 18:53:36      阅读:104      评论:0      收藏:0      [点我收藏+]
public class Son extends Parent {
    
    public String name="jack";
    
    public void init(){
        super.init();
        System.out.println(this.name);
    }
    
    public static void main(String[] args) {
        Son son = new Son();
        son.init();  //init(son)
        System.out.println("## " + son.name);
        
        Parent p = new Son();
        System.out.println("** " + p.name);
        
    }

}

public class Parent {
    
    public String name="tom";

    public void init() {
        System.out.println(this.name);
    }
    
}
————————————————————————————————
public class Parent {

    public void init() {
        System.out.println("1 init parent");
        this.demo();
    }
    
    public void demo() {
        System.out.println("2 demo parent");
    }

}

public class Son extends Parent {
    
    public void init(){
        super.init();
        System.out.println("3 init son");
        this.demo();
    }
    
    public void demo() {
        System.out.println("4 demo Son");
    }
    
    public static void main(String[] args) {
        //当前运行类 Son
        Son son = new Son();
        son.init();  //init(son)
    }

}

以上两种情况运行结果是?为什么?(成员变量和成员方法)

tom,jack,##jack,**tom

1,4,3,4

 

this小案例

原文:http://www.cnblogs.com/cxzdy/p/5570919.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!