this关键字在继承中的使用

this关键字在继承中的使用

参考:http://blog.csdn.net/gxzzxj/article/details/51946144

下面是自己的代码:

public class ChildA extends Father {
    void add() {
        System.out.println("A中的ADD");
    }
}
public class ChildB extends Father {
    void add() {
        System.out.println("B中的ADD");
    }
}
import org.apache.catalina.filters.AddDefaultCharsetFilter;

public class Father {
    public Father() {
        System.out.println(this);
    }
    void add() {
        System.out.println("父类中的ADD");
    }
    
}
public class Main {
    public static void main(String[] args) {
        Father a = new ChildA();
        a.add();
    }
}

 参考博客的代码及结果

public class Father1 {
    private int a = 10;

    Father1() {
        System.out.println(this instanceof Son);
        System.out.println(this.a);
        System.out.println(((Son) this).a);
        System.out.println(this.getClass().getName());
    }

    public static void main(String[] args) {
    
        Father1 s = new Son();
        System.out.println(s.a);
    }
}
class Son extends Father1 {
    public int a = 0;
    public int b = 1;
    public Son() {
        //隐式调用super();
        super();
        System.out.println("Son类中执行:"+this);
    }

}

 

posted on 2017-10-07 02:01 Michael2397 阅读(...) 评论(...) 编辑 收藏

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值