A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

请问this的用法  谁能讲一下啊 不太理解  谢谢

6 个回复

倒序浏览
这个是我的学习笔记,给你参考下

this:看上去,是用于区分局部变量和成员变量同名情况

this为什么可以解决这个问题?
this到底代表的是什么呢?

this:就代表本类对象,到底代表哪一个呢?
        this代表它(this)所在函数的所属对象的引用
        简单说:哪个对象在调用this所在的函数,this就代表哪个对象


this的应用:当定义类中功能时,该函数内部要用到调用该函数的对象时,
这时用this来表示这个对象
        但凡本类功能内部使用到了本类对象,都用this表示,this是对象的引用
class Person
{
        int age;
        Person(int age)
        {
                this.age = age;
        }
        //比较年龄的功能
        public boolean compare(Person p)
        {
                return this.age==p.age;
        }
}

class OOP_4_ThisDemo
{
        public static void main(String[] args)
        {
                Person p = new Person(20);
                Person p1 = new Person(20);
                System.out.println(p.compare(p1));
        }
}


this关键字在构造函数间调用
Person(String name,int age)
{
        //用于构造函数间的互相调用,对 this 的调用必须是构造函数中的第一个语句
        this(name);
        this.age = age;
}

评分

参与人数 1技术分 +1 收起 理由
崔政 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 张会文 于 2013-1-1 18:13 编辑
  1. public class ThisTest {    private int i=0;    //第一个构造器:有一个int型形参   
  2. ThisTest(int i){      
  3. this.i=i+1;//此时this表示引用成员变量i,而非函数参数i      
  4. System.out.println("Int constructor i——this.i:  "+i+"——"+this.i);      
  5. System.out.println("i-1:"+(i-1)+"this.i+1:"+(this.i+1));       //从两个输出结果充分证明了i和this.i是不一样的!    }    //  第二个构造器:有一个String型形参   
  6. ThisTest(String s){       System.out.println("String constructor:  "+s);    }    //  第三个构造器:有一个int型形参和一个String型形参   
  7. ThisTest(int i,String s){      
  8. this(s);//this调用第二个构造器      
  9. //this(i);      
  10. /*此处不能用,因为其他任何方法都不能调用构造器,只有构造方法能调用他。       但是必须注意:就算是构造方法调用构造器,也必须为于其第一行,构造方法也只能调       用一个且仅一次构造器!*/      
  11. this.i=i++;//this以引用该类的成员变量      
  12. System.out.println("Int constructor:  "+i+"\n"+"String constructor:  "+s);   
  13. }   
  14. public ThisTest increment(){      
  15. this.i++;      
  16. return this;//返回的是当前的对象,该对象属于(ThisTest)    }   
  17. public static void main(String[] args){       T
  18. hisTest tt0=new ThisTest(10);      
  19. ThisTest tt1=new ThisTest("ok");      
  20. ThisTest tt2=new ThisTest(20,"ok again!");      
  21. System.out.println(tt0.increment().increment().increment().i);      
  22. //tt0.increment()返回一个在tt0基础上i++的ThisTest对象,      
  23. //接着又返回在上面返回的对象基础上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技术分 +1 收起 理由
崔政 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
这是我收藏的,肯定可以帮到你!
1. this是指当前对象自己。
当在一个类中要明确指出使用对象自己的的变量或函数时就应该加上this引用。如下面这个例子中:
  1. public class Hello {

  2.     String s = "Hello";

  3.     public Hello(String s){
  4.         System.out.println("s = " + s);
  5.         System.out.println("1 -> this.s = " + this.s);
  6.         this.s = s;
  7.         System.out.println("2 -> this.s = " + this.s);
  8.     }

  9.     public static void main(String[] args) {
  10.     Hello x=new Hello("HelloWorld!");
  11.     }
  12. }
复制代码

运行结果:

  1. s = HelloWorld!
  2. 1 -> this.s = Hello
  3. 2 -> this.s = HelloWorld!
复制代码
在这个例子中,构造函数Hello中,参数s与类Hello的变量s同名,这时如果直接对s进行操作则是对参数s进行操作。若要对类Hello的成员变量s进行操作就应该用this进行引用。运行结果的第一行就是直接对构造函数中传递过来的参数s进行打印结果; 第二行是对成员变量s的打印;第三行是先对成员变量s赋传过来的参数s值后再打印,所以结果是HelloWorld!
2. 把this作为参数传递 当你要把自己作为参数传递给别的对象时,也可以用this。如:
  1. public class A {
  2.   public A() {
  3.     new B(this).print();
  4.   }

  5.   public void print() {
  6.     System.out.println("Hello from A!");
  7.   }
  8. }

  9. public class B {
  10.   A a;
  11.   public B(A a) {
  12.     this.a = a;
  13.   }

  14.   public void print() {
  15.     a.print();
  16.     System.out.println("Hello from B!");
  17.   }
  18. }

  19. 运行结果:
  20.   Hello from A!
  21.   Hello from B!
复制代码
在这个例子中,对象A的构造函数中,用new B(this)把对象A自己作为参数传递给了对象B的构造函数。
3. 注意匿名类和内部类中的中的this。 有时候,我们会用到一些内部类和匿名类,如事件处理。当在匿名类中用this时,这个this则指的是匿名类或内部类本身。这时如果我们要使用外部类的方法和变量的话,则应该加上外部类的类名。如下面这个例子:
  1. public class A {
  2.     int i = 1;

  3.     public A() {
  4.         Thread thread = new Thread() {
  5.             public void run() {
  6.                 for(;;) {
  7.                     A.this.run();
  8.                     try {
  9.                         sleep(1000);
  10.                     } catch(InterruptedException ie) {     }

  11.                 }
  12.            }
  13.         }; //注意这里有;
  14.         thread.start();
  15.     }

  16.     public void run() {
  17.         System.out.println("i = " + i);
  18.         i++;
  19.     }

  20.     public static void main(String[] args) throws Exception {
  21.         new A();
  22.     }
  23. }
复制代码
在上面这个例子中, thread 是一个匿名类对象,在它的定义中,它的 run 函数里用到了外部类的 run 函数。这时由于函数同名,直接调用就不行了。这时有两种办法,一种就是把外部的 run 函数换一个名字,但这种办法对于一个开发到中途的应用来说是不可取的。那么就可以用这个例子中的办法用外部类的类名加上 this 引用来说明要调用的是外部类的方法 run。
4。在构造函数中,通过this可以调用同一class中别的构造函数,如
  1. public class Flower{
  2.            Flower (int petals){}
  3.            Flower(String ss){}
  4.            Flower(int petals, Sting ss){
  5.                 //petals++;调用另一个构造函数的语句必须在最起始的位置
  6.                 this(petals);
  7.                //this(ss);会产生错误,因为在一个构造函数中只能调用一个构造函数
  8.            }
  9. }
复制代码
值得注意的是:
1:在构造调用另一个构造函数,调用动作必须置于最起始的位置。
2:不能在构造函数以外的任何函数内调用构造函数。
3:在一个构造函数内只能调用一个构造函数。


评分

参与人数 1技术分 +1 收起 理由
崔政 + 1

查看全部评分

回复 使用道具 举报
2l楼说的好,学习了。
这个是我的学习笔记,给你参考下

this:看上去,是用于区分局部变量和成员变量同名情况

this为什么可以解决这个问题?
this到底代表的是什么呢?

this:就代表本类对象,到底代表哪一个呢?
        this代表它(this)所在函数的所属对象的引用
        简单说:哪个对象在调用this所在的函数,this就代表哪个对象


this的应用:当定义类中功能时,该函数内部要用到调用该函数的对象时,
这时用this来表示这个对象
        但凡本类功能内部使用到了本类对象,都用this表示,this是对象的引用
class Person
{
        int age;
        Person(int age)
        {
                this.age = age;
        }
        //比较年龄的功能
        public boolean compare(Person p)
        {
                return this.age==p.age;
        }
}

class OOP_4_ThisDemo
{
        public static void main(String[] args)
        {
                Person p = new Person(20);
                Person p1 = new Person(20);
                System.out.println(p.compare(p1));
        }
}


this关键字在构造函数间调用
Person(String name,int age)
{
        //用于构造函数间的互相调用,对 this 的调用必须是构造函数中的第一个语句
        this(name);
        this.age = age;
}

评分

参与人数 1技术分 +1 收起 理由
崔政 + 1 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马