搜索
首页JavaJava入门java中this关键字怎么使用?

java中this关键字怎么使用?

Nov 02, 2020 pm 04:30 PM
javathis关键字

java中this关键字的用法:1、当成员变量和局部变量重名时,在方法中使用this时,表示的是该方法所在类中的成员变量;2、在构造函数中,通过this可以调用同一类中别的构造函数;3、使用this同时传递多个参数。

java中this关键字怎么使用?

相关推荐:《Java视频教程

1、当成员变量和局部变量重名时,在方法中使用this时,表示的是该方法所在类中的成员变量。(this是当前对象自己)

public class Hello {
    String s = "Hello"; 
    public Hello(String s) {
       System.out.println("s = " + s);
       System.out.println("1 -> this.s = " + this.s);       
       this.s = s;//把参数值赋给成员变量,成员变量的值改变
       System.out.println("2 -> this.s = " + this.s);
    } 
    public static void main(String[] args) {
       Hello x = new Hello("HelloWorld!");
       System.out.println("s=" + x.s);//验证成员变量值的改变    }
}

结果为:

s = HelloWorld!
1 -> this.s = Hello
2 -> this.s = HelloWorld!
s=HelloWorld!

在这个例子中,构造函数Hello中,参数s与类Hello的成员变量s同名,这时如果直接对s进行操作则是对参数s进行操作。若要对类Hello的成员变量s进行操作就应该用this进行引用。运行结果的第一行就是直接对构造函数中传递过来的参数s进行打印结果; 第二行是对成员变量s的打印;第三行是先对成员变量s赋传过来的参数s值后再打印,所以结果是HelloWorld!而第四行是主函数中直接打印类中的成员变量的值,也可以验证成员变量值的改变。

2、把自己当作参数传递时,也可以用this.(this作当前参数进行传递)

class A {
    public A() {
       new B(this).print();// 调用B的方法
    }
    public void print() {
       System.out.println("HelloAA from A!");
    }
}
class B {
    A a;
    public B(A a) {
       this.a = a;
    }
    public void print() {
       a.print();//调用A的方法
       System.out.println("HelloAB from B!");
    }
}
public class HelloA {
    public static void main(String[] args) {
       A aaa = new A();
       aaa.print();
       B bbb = new B(aaa);
       bbb.print();
    }
}

结果为:

HelloAA from A!
HelloAB from B!
HelloAA from A!
HelloAA from A!
HelloAB from B!

在这个例子中,对象A的构造函数中,用new B(this)把对象A自己作为参数传递给了对象B的构造函数。

3、有时候,我们会用到一些内部类和匿名类,如事件处理。当在匿名类中用this时,这个this则指的是匿名类或内部类本身。这时如果我们要使用外部类的方法和变量的话,则应该加上外部类的类名。如:

public class HelloB {
    int i = 1;
 
    public HelloB() {
       Thread thread = new Thread() {
           public void run() {
              for (int j=0;j<20;j++) {
                  HelloB.this.run();//调用外部类的方法
                  try {
                     sleep(1000);
                  } catch (InterruptedException ie) {
                  }
              }
           }
       }; // 注意这里有分号
       thread.start();
    }
 
    public void run() {
       System.out.println("i = " + i);
       i++;
    }
   
    public static void main(String[] args) throws Exception {
       new HelloB();
    }
}

在上面这个例子中, thread 是一个匿名类对象,在它的定义中,它的 run 函数里用到了外部类的 run 函数。这时由于函数同名,直接调用就不行了。这时有两种办法,一种就是把外部的 run 函数换一个名字,但这种办法对于一个开发到中途的应用来说是不可取的。那么就可以用这个例子中的办法用外部类的类名加上 this 引用来说明要调用的是外部类的方法 run。

4、在构造函数中,通过this可以调用同一类中别的构造函数。如:

public class ThisTest {
    ThisTest(String str) {
       System.out.println(str);
    }
    ThisTest() {
       this("this测试成功!");
    }
 
    public static void main(String[] args) {
       ThisTest thistest = new ThisTest();
    }
}

为了更确切的说明this用法,另外一个例子为:

public class ThisTest {
    private int age;
    private String str;
 
    ThisTest(String str) {
       this.str=str;
       System.out.println(str);
    }
    ThisTest(String str,int age) {
       this(str);
       this.age=age;
       System.out.println(age);
    }
 
    public static void main(String[] args) {
       ThisTest thistest = new ThisTest("this测试成功",25);
      
    }
}

结果为:

this测试成功
25

值得注意的是: 
  1:在构造调用另一个构造函数,调用动作必须置于最起始的位置。 
  2:不能在构造函数以外的任何函数内调用构造函数。 
  3:在一个构造函数内只能调用一个构造函数。

5、this同时传递多个参数。

public class TestClass {
    int x;
    int y;
 
    static void showtest(TestClass tc) {//实例化对象
       System.out.println(tc.x + " " + tc.y);
    }
    void seeit() {
       showtest(this);
    }
 
    public static void main(String[] args) {
       TestClass p = new TestClass();
       p.x = 9;
       p.y = 10;
       p.seeit();
    }
}

结果为:

9 10

代码中的showtest(this),这里的this就是把当前实例化的p传给了showtest()方法,从而就运行了。

更多编程相关知识,请访问:编程入门!!

以上是java中this关键字怎么使用?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具