this关键字
来源:互联网 发布:linux udp网络编程 编辑:程序博客网 时间:2024/04/29 00:47
一.this是一个隐式参数(另一个隐式参数super在博文《继承》中详细介绍)
每一个普通方法被调用时,会默认隐式的传递一个参数,这个参数就是调用该方法的对象 (也就是对象地址,也就是this).比如对象x调用,就隐式的把x对象的地址传过去.既this是方法中的一个参数,虽然是隐式的,但是可以当作参数去使用.
因此this关键字只能在方法内部使用,表示对”调用该方法的对象”的引用.
二.this表示的是什么?
- 普通方法中,this总是指向调用该方法的对象
- 构造方法中,this总是指向正要初始化的对象
- this不能用于static方法(因为this指向对象,而static方法是类方法,不存在对象中,而且static方法运行时不会传递this参数)(有关static关键字的内容,详情见博文《static关键字》)
三.this的四种用法
1.引用成员变量
例如:
public class ThisTest { private String name; private int age; public ThisTest(String name,int age){ this.name = name; this.age = age; } public void setName(String name){ this.name = name; } public void setAge(int age){ this.age = age; }}
2.返回当前对象的引用
例如:
public class ThisTest { private int i = 0; public ThisTest increment(){ i++; return this; } public void print(){ System.out.println("i="+i); } public static void main(String[] args) { ThisTest x = new ThisTest(); x.increment().increment().increment().print(); } }/* *输出结果为:i=3*/
3.在构造方法中调用其他构造方法(类中有多个构造方法时)
例如:
public class ThisTest { public static void main(String[] args) { ThisTest x = new ThisTest(); x.printPetalCount(); } private int petalCount = 0; private String s = "initial value"; public ThisTest(){ this("hi",47); System.out.println("default constructor (no args)"); } public ThisTest(String s,int petals){ this(petals); //!this(s); //this不能在一个构造器中调用两个构造器 this.s = s; System.out.println("String & int args"); } public ThisTest(int petals){ petalCount = petals; System.out.println("Constructor w/ int arg only.petalCount="+petalCount); } public void printPetalCount(){ //!this(11); //只能在构造方法中调用构造方法,其他方法都不能调用构造方法(编译报错) System.out.println("petalCount="+petalCount+"s="+s); }}/*输出结果: *Constructor w/ int arg only.petalCount=47 *String & int args *default constructor (no args) *petalCount=47s=hi*/
4.将对象传递给其他方法
例如:
public class ThisTest { public static void main(String[] args) { new Person().eat(new Apple()); }}class Person{ public void eat(Apple apple){ Apple peeled = apple.getPeeled(); System.out.println("Yummy"); }}class Peeler{ static Apple peel(Apple apple){ return apple; }}class Apple{ Apple getPeeled(){ return Peeler.peel(this); }}/* *输出结果:Yummy*/
注意:
- this可以调用构造方法,但不能在一个构造方法中调用两个构造方法,否则编译报错
- this调用构造方法,必须在构造方法的第一句,否则编译报错
- 只能在构造方法中调用构造方法,其他方法都不能调用构造方法,否则编译报错
5.有时候,我们会用到一些内部类和匿名类,如事件处理。当在匿名类中用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。
1 0
- 关键字this
- this关键字
- this关键字
- this 关键字
- this 关键字
- this关键字
- this关键字
- this 关键字
- this 关键字
- this 关键字
- this关键字
- 关键字this
- 关键字this
- this 关键字
- this 关键字
- this关键字
- this关键字
- This关键字
- 数据库同步工具:ETL使用说明
- 利用 QGA 来助力 OpenStack 私有云
- Android 自定义Toolbar/ActionBar视图左右两边有空白
- Android来电自动接听,自动录音,自动回拨功能
- Maven详解之仓库------本地仓库、远程仓库
- this关键字
- 动态规划总结
- QTP对象的方法(一)
- spring boot jar 部署linux服务器
- React/React Native 的ES5 ES6写法对照表
- 字符串初始化与其在用户栈中的溢出
- 二维数组和指向指针的指针
- Fragment在ViewPager中布局文件丢失
- Objective-C Runtime 运行时之一:类与对象