this小案例

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通常是具体实现类对象,用途可以覆盖父类,没覆盖的话就执行父类的这个方法

 

public abstract class MGenericServlet2 implements Servlet,ServletConfig {
    
    private ServletConfig config;
    
    
    public void init(ServletConfig config) throws ServletException {
        //保存当前servlet的初始化信息
        this.config = config;
        System.out.println("@@@@init");
        this.init();
    }
    
    public void init() throws ServletException {
        
    }
}

。。。。。。。。。。。。。
public class DemoServlet extends MGenericServlet2 {
    
    //进行初始化操作
//    @Override
//    public void init(ServletConfig config) throws ServletException {
//        //父类初始化
//        super.init(config);
//        //子类初始化
//        //xxxxx
//    
//    }
    
    
    @Override
    public void init() throws ServletException {
        System.out.println("........");
    }
}

 

转载于:https://www.cnblogs.com/cxzdy/p/5570919.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值