java关键字this的使用

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));

       //从两个输出结果充分证明了ithis.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?






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值