设为首页 加入收藏

TOP

Java之this关键字
2014-11-23 20:01:26 】 浏览:9582
Tags:Java this 关键字

this使用范围  


1、在类的方法定义中使用的this关键字代表调用该方法对象的引用。


2、当必须指出当前使用方法的对象是谁时,要使用关键字this。


3、有时使用this可以处理方法中成员变量和参数重名的情况。


4、this可以看做是一个变量,它的值是当前对象的引用。


注:this一般出现在方法中,当方法没有被调用时。并不知道this指向那个具体的对象。


当某个对象调用有this的方法时,this就指向调用这个方法的对象。


程序code:


public class TestThis{
private int i;
public TestThis(int i){
this.i = i;
}
private TestThis increment(){
i += 1;
return this;
}

public static void main (String[] args){
TestThis mTestThis = new TestThis(100);
System.out.println(mTestThis.increment().increment().i);
}
}


输出结果:102



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java之static关键字 下一篇AngularJS 中的通信(发布订阅模式)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目