Java中的this指针使用方法分享
Java中,一般来说this指针指的是当前正在访问的这段代码的对象,但是如果在内部类中需要使用外部类中的对象,这时就需要使用外部类的类名进行限定。这种方式在Android开发中也比较常见。
@Author:twlkyao
packagetwlkyao;
publicclassA{
publicA(){
Innerinner=newInner();
inner.outer();//calltheinnerclass'soutermethod.
this.outer();//callA'soutermethod.
}
publicvoidouter(){
System.out.println("outerrun");
}
classInner{
publicvoidouter(){
System.out.println("innerrun");
A.this.outer();//callA'soutermethod.
System.out.println("--------");
}
}
publicstaticvoidmain(String[]args){
Aa=newA();
}
}
Inner是内部类,访问类A中的outer()方法,又由于匿名内部类中有同样的方法,所以需要使用A的this指针进行限定。
输出结果为:
innerrun outerrun -------- outerrun
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短