java关键字this的使用
2016-07-22 22:42
429 查看
在JAVA程序中似乎经常见到“this”,自己也偶尔用到它,但是到底“this”该怎么用,却心中无数!很多人一提起它,就说“当前对象”,可到底什么是当前对象,是什么当前对象,他自己也不清楚。现在让大家看一个小例子,给你分享一下JAVA中“this”的用法!
/**
* @author fengzhi-neusoft
*
* 本示例为了说明this的三种用法!
*/
package test;
public class ThisTest {
private int i=0;
//第一个构造器:有一个int型形参
ThisTest(int i){
this.i=i+1;//此时this表示引用成员变量i,而非函数参数i
System.out.println("Int constructor i——this.i: "+i+"——"+this.i);
System.out.println("i-1:"+(i-1)+"this.i+1:"+(this.i+1));
//从两个输出结果充分证明了i和this.i是不一样的!
}
// 第二个构造器:有一个String型形参
ThisTest(String s){
System.out.println("String constructor: "+s);
}
// 第三个构造器:有一个int型形参和一个String型形参
ThisTest(int i,String s){
this(s);//this调用第二个构造器
//this(i);
/*此处不能用,因为其他任何方法都不能调用构造器,只有构造方法能调用他。
但是必须注意:就算是构造方法调用构造器,也必须为于其第一行,构造方法也只能调
用一个且仅一次构造器!*/
this.i=i++;//this以引用该类的成员变量
System.out.println("Int constructor: "+i+"/n"+"String
constructor: "+s);
}
public ThisTest increment(){
this.i++;
return this;//返回的是当前的对象,该对象属于(ThisTest)
}
public static void main(String[]
args){
ThisTest tt0=new ThisTest(10);
ThisTest tt1=new ThisTest("ok");
ThisTest tt2=new ThisTest(20,"ok again!");
System.out.println(tt0.increment().increment().increment().i);
//tt0.increment()返回一个在tt0基础上i++的ThisTest对象,
//接着又返回在上面返回的对象基础上i++的ThisTest对象!
}
}
运行结果:
Int constructor i——this.i: 10——11
String constructor: ok
String constructor: ok again!
Int constructor: 21
String constructor: ok again!
14
细节问题注释已经写的比较清楚了,这里不在赘述,只是总结一下,其实this主要要三种用法:
1、表示对当前对象的引用!
2、表示用类的成员变量,而非函数参数,注意在函数参数和成员变量同名是进行区分!其实这是第一种用法的特例,比较常用,所以那出来强调一下。
3、用于在构造方法中引用满足指定参数类型的构造器(其实也就是构造方法)。但是这里必须非常注意:只能引用一个构造方法且必须位于开始!
还有就是注意:this不能用在static方法中!所以甚至有人给static方法的定义就是:没有this的方法!虽然夸张,但是却充分说明this不能在static方法中使用!
_____________________________________________________________________________________________
1) To specifically denote that the instance variable is used instead of static orlocal
variable.That is,
Here this refers to the instance variable. Here the precedence is high for the local variable. Therefore the absence of the “this” denotes the local variable. If the local variable that is parameter’s name is not same as instance variable then irrespective
of this is used or not it denotes the instance variable.
2) Java This is used to refer the constructors
Java This invokes the constructor of the same java class which has two parameters.
3) Java This is used to pass the current java instance as
parameter
4) Similar to the above, java this can also be used to return the current instance
Note: This may lead to undesired results while used in inner classes in the above two points. Since this will refer to the inner
class and not the outer instance.
5) Java This can be used to get the handle of the current class
Though this can be done by, Class className = ABC.class; // here ABC refers to the class name and you need to know that!
As always, java this is associated with its instance and this will not work in static methods.
How is(java) this?
/**
* @author fengzhi-neusoft
*
* 本示例为了说明this的三种用法!
*/
package test;
public class ThisTest {
private int i=0;
//第一个构造器:有一个int型形参
ThisTest(int i){
this.i=i+1;//此时this表示引用成员变量i,而非函数参数i
System.out.println("Int constructor i——this.i: "+i+"——"+this.i);
System.out.println("i-1:"+(i-1)+"this.i+1:"+(this.i+1));
//从两个输出结果充分证明了i和this.i是不一样的!
}
// 第二个构造器:有一个String型形参
ThisTest(String s){
System.out.println("String constructor: "+s);
}
// 第三个构造器:有一个int型形参和一个String型形参
ThisTest(int i,String s){
this(s);//this调用第二个构造器
//this(i);
/*此处不能用,因为其他任何方法都不能调用构造器,只有构造方法能调用他。
但是必须注意:就算是构造方法调用构造器,也必须为于其第一行,构造方法也只能调
用一个且仅一次构造器!*/
this.i=i++;//this以引用该类的成员变量
System.out.println("Int constructor: "+i+"/n"+"String
constructor: "+s);
}
public ThisTest increment(){
this.i++;
return this;//返回的是当前的对象,该对象属于(ThisTest)
}
public static void main(String[]
args){
ThisTest tt0=new ThisTest(10);
ThisTest tt1=new ThisTest("ok");
ThisTest tt2=new ThisTest(20,"ok again!");
System.out.println(tt0.increment().increment().increment().i);
//tt0.increment()返回一个在tt0基础上i++的ThisTest对象,
//接着又返回在上面返回的对象基础上i++的ThisTest对象!
}
}
运行结果:
Int constructor i——this.i: 10——11
String constructor: ok
String constructor: ok again!
Int constructor: 21
String constructor: ok again!
14
细节问题注释已经写的比较清楚了,这里不在赘述,只是总结一下,其实this主要要三种用法:
1、表示对当前对象的引用!
2、表示用类的成员变量,而非函数参数,注意在函数参数和成员变量同名是进行区分!其实这是第一种用法的特例,比较常用,所以那出来强调一下。
3、用于在构造方法中引用满足指定参数类型的构造器(其实也就是构造方法)。但是这里必须非常注意:只能引用一个构造方法且必须位于开始!
还有就是注意:this不能用在static方法中!所以甚至有人给static方法的定义就是:没有this的方法!虽然夸张,但是却充分说明this不能在static方法中使用!
_____________________________________________________________________________________________
1) To specifically denote that the instance variable is used instead of static orlocal
variable.That is,
private String javaFAQ; void methodName(String javaFAQ) { this.javaFAQ = javaFAQ; }
Here this refers to the instance variable. Here the precedence is high for the local variable. Therefore the absence of the “this” denotes the local variable. If the local variable that is parameter’s name is not same as instance variable then irrespective
of this is used or not it denotes the instance variable.
2) Java This is used to refer the constructors
public JavaQuestions(String javapapers) { this(javapapers, true); }
Java This invokes the constructor of the same java class which has two parameters.
3) Java This is used to pass the current java instance as
parameter
obj.itIsMe(this);
4) Similar to the above, java this can also be used to return the current instance
CurrentClassName startMethod() { return this; }
Note: This may lead to undesired results while used in inner classes in the above two points. Since this will refer to the inner
class and not the outer instance.
5) Java This can be used to get the handle of the current class
Class className = this.getClass(); // this methodology is preferable in java
Though this can be done by, Class className = ABC.class; // here ABC refers to the class name and you need to know that!
As always, java this is associated with its instance and this will not work in static methods.
How is(java) this?
相关文章推荐
- springmvc在线教程
- springmvc在线教程
- SpringMVC集成rabbitMQ
- Java基础之(十)基本类型的类型转换
- JAVA 面向对象 隐藏和封装
- Java多线程——sychronized
- Java相关环境变量配置总结
- SpringMVC工作原理解读--DispatcherServlet类源码解读(初级)
- JAVA 中 Arrays.sort 中的排序的方法
- JAVA常用的类库
- java学习之路-基础语法-运算符
- Struts1知识点、注意事项总结
- 基数排序简单Java实现
- springMVC和Shiro框架整合使用简单示例
- Java ConcurrentModificationException异常原因和解决方法
- linphone-LinphoneCoreFactory.java文件分析
- Java编程思想学习笔记_4(异常机制,容器)
- java非阻塞笔记一
- JAVA面向对象-----this的概述
- JAVA面向对象-----this的概述