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关键字的几种用法的更多相关文章

  1. java中 this 关键字的三种用法

    Java中this的三种用法 调用属性 (1)this可以调用本类中的任何成员变量 调用方法(可省略) (2)this调用本类中的成员方法(在main方法里面没有办法通过this调用) 调用构造方法 ...

  2. Java中super关键字的作用与用法

    Java中的super是什么?java中的super关键字是一个引用变量,用于引用父类对象.关键字“super”以继承的概念出现在类中.主要用于以下情况: 1.使用super与变量:当派生类和基类具有 ...

  3. Java中static关键字的作用和用法详细介绍

    static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static修饰的成员变量和成员方法独立于该类的任何 ...

  4. Java中Array.sort()的几种用法

    ****************************************************** * 精品书籍推荐:<Java从入门到经通> * 本书系统全面.浅显易懂,非常适 ...

  5. 【转】C#中base关键字的几种用法

    base其实最大的使用地方在面相对性开发的多态性上,base可以完成创建派生类实例时调用其基类构造函数或者调用基类上已被其他方法重写的方法.例如: 2.1关于base调用基类构造函数 public c ...

  6. (转)C#中base关键字的几种用法

    base其实最大的使用地方在面相对性开发的多态性上,base可以完成创建派生类实例时调用其基类构造函数或者调用基类上已被其他方法重写的方法.例如: 2.1关于base调用基类构造函数 public c ...

  7. Java中关于枚举的7种用法

    1.定义常量: public enum Color { RED,ORANGE,YELLOW,GREEN,INDIGO,BLUE,PURPLE } 2.用于switch: enum Color { RE ...

  8. 【转】C#中base关键字的几种用法:base()

    转:https://blog.csdn.net/cplvfx/article/details/82982862 base其实最大的使用地方在面相对象开发的多态性上,base可以完成创建派生类实例时调用 ...

  9. Java中Array.sort()的几种用法(需要初始化要排序的对象)

    ====================================================== 1.Arrays.sort(int[] a) 这种形式是对一个数组的所有元素进行排序,并且 ...

随机推荐

  1. 记一次小团队Git实践(上)

    公司规模不大,成立之初,选择了svn作为版本控制系统.这对于用惯了git的我来说,将就了一段时间后,极为不爽,切换到git-svn勉强能用.随后,因为产品需要发布不同的版本,而git-svn对远程分支 ...

  2. JSON转javabean(pojo)利器

    别再对着json来手写javabean啦.这个工作完全不要脑子,而且耗时. 这里给大家提供三种方式: android studio版: 万能的插件:GsonFormat 如何安装? Preferenc ...

  3. TCP粘包/拆包问题

    无论是服务端还是客户端,当我们读取或者发送消息的时候,都需要考虑TCP底层的粘包/拆包机制. TCP粘包/拆包 TCP是个"流"协议,所谓流,就是没有界限的一串数据.大家可以想想河 ...

  4. python 线程之_thread

    python 线程之_thread _thread module: 基本用法: def child(tid): print("hello from child",tid) _thr ...

  5. iOS Outlets Referencing Outlets

    一位网友的解释: 原址:http://www.cocoachina.com/bbs/read.php?tid-21295.html

  6. HTML DOM Document

    Document 对象 每个载入浏览器的 HTML 文档都会成为 Document 对象. Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问. 提示:Document 对 ...

  7. DOM基础2

    插入元素 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...

  8. tomcat与HTML命令提示符

    在tomcatwebapps目录下建立一个新文件夹 命名为my 把第一个学习的HTML文件放到my文件夹内 通过tomcat服务器远程访问该网页 把localhost换成自己的IP地址 先查看自己的I ...

  9. UVa12092 Paint the Roads(最小费用最大流)

    题目大概说一个n个点m条带权有向边的图,要给边染色,染色的边形成若干个回路且每个点都恰好属于其中k个回路.问最少要染多少边权和的路. 一个回路里面各个点的入度=出度=1,那么可以猜想知道各个点如果都恰 ...

  10. OA

    OA(政府) http://zw.wuzhitong.com/oa_wuzhitong/index.asp http://www.wuzhitong.com/Information_ZW.html O ...