zoukankan      html  css  js  c++  java
  • 转:JAVA中this用法小结

    转:http://blog.sina.com.cn/s/blog_6a6badc90100t8hm.html#SinaEditor_Temp_FontName

      Java关键字this只能用于方法方法体内。当一个对象创建后,Java虚拟机(JVM)就会给这个对象分配一个引用自身的指针,这个指针的名字就是 this。因此,this只能在类中的非静态方法中使用,静态方法和静态的代码块中绝对不能出现this,这在“Java关键字static、final 使用总结”一文中给出了明确解释。并且this只和特定的对象关联,而不和类关联,同一个类的不同对象有不同的this。

      

     1 package test;
     2 public class ThisTest {
     3     private int i=0;
     4     //第一个构造器:有一个int型形参
     5     ThisTest(int i){
     6        this.i=i+1;//此时this表示引用成员变量i,而非函数参数i
     7        System.out.println("Int constructor i——this.i:  "+i+"——"+this.i);
     8        System.out.println("i-1:"+(i-1)+"this.i+1:"+(this.i+1));
     9        //从两个输出结果充分证明了i和this.i是不一样的!
    10     }
    11     //  第二个构造器:有一个String型形参
    12     ThisTest(String s){
    13        System.out.println("String constructor:  "+s);
    14     }
    15     //  第三个构造器:有一个int型形参和一个String型形参
    16     ThisTest(int i,String s){
    17        this(s);//this调用第二个构造器
    18        //this(i);
    19       
    20        this.i=i++;//this以引用该类的成员变量
    21        System.out.println("Int constructor:  "+i+"/n"+"String constructor:  "+s);
    22     }
    23     public ThisTest increment(){
    24        this.i++;
    25        return this;//返回的是当前的对象,该对象属于(ThisTest)
    26     }
    27     public static void main(String[] args){
    28        ThisTest tt0=new ThisTest(10);
    29        ThisTest tt1=new ThisTest("ok");
    30        ThisTest tt2=new ThisTest(20,"ok again!");
    31       
    32        System.out.println(tt0.increment().increment().increment().i);
    33        //tt0.increment()返回一个在tt0基础上i++的ThisTest对象,
    34        //接着又返回在上面返回的对象基础上i++的ThisTest对象!
    35     }
    36 }

    运行结果:

    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方法中使用!

     

    说明在什么情况下需要用到this
            第一、通过this调用另一个构造方法,用发是this(参数列表),这个仅仅在类的构造方法中,别的地方不能这么用。
            第二、函数参数或者函数中的局部变量和成员变量同名的情况下,成员变量被屏蔽,此时要访问成员变量则需要用“this.成员变量名”的方式来引用成员变量。当然,在没有同名的情况下,可以直接用成员变量的名字,而不用this,用了也不为错,呵呵。
            第三、在函数中,需要引用该函所属类的当前对象时候,直接用this。

            其实这些用法总结都是从对“this是指向对象本身的一个指针”这句话的更深入的理解而来的,死记不然容易忘记而且容易搞错,要理解!
     
     
     
     

     

  • 相关阅读:
    poj 3616 Milking Time
    poj 3176 Cow Bowling
    poj 2229 Sumsets
    poj 2385 Apple Catching
    poj 3280 Cheapest Palindrome
    hdu 1530 Maximum Clique
    hdu 1102 Constructing Roads
    codeforces 592B The Monster and the Squirrel
    CDOJ 1221 Ancient Go
    hdu 1151 Air Raid(二分图最小路径覆盖)
  • 原文地址:https://www.cnblogs.com/yaowukonga/p/3801455.html
Copyright © 2011-2022 走看看