• Java this关键字详解


    this 关键字用来表示当前对象本身,或当前类的一个实例,通过 this 可以调用本对象的所有方法和属性。例如:
    1. public class Demo{
    2. public int x = 10;
    3. public int y = 15;
    4. public void sum(){
    5. // 通过 this 点取成员变量
    6. int z = this.x + this.y;
    7. System.out.println("x + y = " + z);
    8. }
    9. public static void main(String[] args) {
    10. Demo obj = new Demo();
    11. obj.sum();
    12. }
    13. }
    运行结果:
    x + y = 25

    上面的程序中,obj 是 Demo 类的一个实例,this 与 obj 等价,执行 int z = this.x + this.y;,就相当于执行 int z = obj.x + obj.y;。

    注意:this 只有在类实例化后才有意义。

    使用this区分同名变量

    成员变量与方法内部的变量重名时,希望在方法内部调用成员变量,怎么办呢?这时候只能使用this,例如:
    1. public class Demo{
    2.     public String name;
    3.     public int age;
    4.   
    5.     public Demo(String name, int age){
    6.         this.name = name;
    7.         this.age = age;
    8.     }
    9.   
    10.     public void say(){
    11.         System.out.println("网站的名字是" + name + ",已经成立了" + age + "年");
    12.     }
    13.   
    14.     public static void main(String[] args) {
    15.         Demo obj = new Demo("微学苑", 3);
    16.         obj.say();
    17.     }
    18. }
    运行结果:
    网站的名字是微学苑,已经成立了3年

    形参的作用域是整个方法体,是局部变量。在Demo()中,形参和成员变量重名,如果不使用this,访问到的就是局部变量name和age,而不是成员变量。在 say() 中,我们没有使用 this,因为成员变量的作用域是整个实例,当然也可以加上 this:
    1. public void say(){
    2. System.out.println("网站的名字是" + this.name + ",已经成立了" + this.age + "年");
    3. }
    Java 默认将所有成员变量和成员方法与 this 关联在一起,因此使用 this 在某些情况下是多余的。

    作为方法名来初始化对象

    也就是相当于调用本类的其它构造方法,它必须作为构造方法的第一句。示例如下:
    1. public class Demo{
    2. public String name;
    3. public int age;
    4. public Demo(){
    5. this("微学苑", 3);
    6. }
    7. public Demo(String name, int age){
    8. this.name = name;
    9. this.age = age;
    10. }
    11. public void say(){
    12. System.out.println("网站的名字是" + name + ",已经成立了" + age + "年");
    13. }
    14. public static void main(String[] args) {
    15. Demo obj = new Demo();
    16. obj.say();
    17. }
    18. }
    运行结果:
    网站的名字是微学苑,已经成立了3年

    值得注意的是:
    • 在构造方法中调用另一个构造方法,调用动作必须置于最起始的位置。
    • 不能在构造方法以外的任何方法内调用构造方法。
    • 在一个构造方法内只能调用一个构造方法。

    上述代码涉及到方法重载,即Java允许出现多个同名方法,只要参数不同就可以。后续章节会讲解。

    作为参数传递

    需要在某些完全分离的类中调用一个方法,并将当前对象的一个引用作为参数传递时。例如:
    1. public class Demo{
    2. public static void main(String[] args){
    3. B b = new B(new A());
    4. }
    5. }
    6. class A{
    7. public A(){
    8. new B(this).print(); // 匿名对象
    9. }
    10. public void print(){
    11. System.out.println("Hello from A!");
    12. }
    13. }
    14. class B{
    15. A a;
    16. public B(A a){
    17. this.a = a;
    18. }
    19. public void print() {
    20. a.print();
    21. System.out.println("Hello from B!");
    22. }
    23. }
    运行结果:
    Hello from A!
    Hello from B!

    匿名对象就是没有名字的对象。如果对象只使用一次,就可以作为匿名对象,代码中 new B(this).print(); 等价于 ( new B(this) ).print();,先通过 new B(this) 创建一个没有名字的对象,再调用它的方法。
    Java辅导班
    <上一节下一节>
    被顶起来的评论
    • .源代码
      .源代码

      package thisfangfa;
      public class This {
      public static void main(String[] args){
      B b=new B(new A());//这里创建B类的对象b时,对B类的构造函数传入了
      //A类的对象的引用(指针),
      }
      }
      class A{
      public A()
      {
      System.out.println www.uuweb.cn ("step 1 ");
      new B(this).print();//程序第一步会执行A类的构造方法A(),因为创建B类
      //的对象b时,对B类的构造函数传入了A类的对象的引用(指针),所以程序先执行
      //new A() ,执行new A()的时候就运行了A类的构造方法。A类的构造方法里面
      //创建了B类的匿名对象,并且引用了B类匿名对象的print();方法。
      }
      public void print()
      {
      System.out.println("step 3");//由于第二步a.print();执行了A类的
      //print()方法。所以程序第三步会运行到这里。
      System.out.println("hello from A");//然后执行到这句
      //代码,所以打印输出了hello from A
      }
      }
      class B{
      A a;//这里先声明一个A类的对象a,这时还没有对a实例化,只是声明。
      public B(A a){//执行new A()完毕后,开始执行执行new B()了,这时会执行B类的
      //构造函数B(),
      this.a=a;//B b=new B(new A());//这里创建B类的对象b时,传入的A类对象
      //的引用(指针),赋予了之前声明的A类的对象a,这时a存储着创建B类的对象b时,传
      //入的A类对象的引用(指针)。
      }
      public void print()
      { System.out.println("step 2 ");//由于引用了B类匿名对象的print()方法。
      //所以程序第二步会执行到这个方法里面。
      a.print();//因为a存储着创建B类的对象b时,传入的A类对象的引用(指针)。
      //所以这里执行a对象的方法print(),就是执行的A这个类里面的
      //print()方法。所以程序第二步会运行到这里。这里执行了A类的print()方法。
      //
      //打印输出hello from A完毕后,a.print();执行完毕,
      System.out.println("step 4 ");//所以开始执行这句
      System.out.println("hello from B");//和执行这句
      //整个程序结束在这里。
      }
      }

      在Eclipse下程序输出结果是这样的:

      step 1 
      step 2 
      step 3
      hello from A
      step 4 
      hello from B

  • 相关阅读:
    Python深入05 装饰器
    Python深入04 闭包
    Python深入03 对象的属性
    Ubuntu (虚拟机同样) 更换内核?
    .out
    GCC 编译详解
    linux 编译内核 /boot空间不足?
    Java Swing提供的文件选择对话框
    Java Swing 实时刷新JTextArea,以显示不断append的内容?
    为什么要编译Linux内核?
  • 原文地址:https://www.cnblogs.com/ok932343846/p/6749469.html
Copyright © 2020-2023  润新知