This of Java keyword
•
Java
This of Java keyword
concept
This keyword can only be used inside a method to refer to the "object calling the method". Specifically. There are two situations:
package com.my.pac09;
/*
* ThisTest.java
* this 对“调用方法那个对象”的引用
*/
public class ThisTest {
private int height;
public int width;
public void setHeight(int height) {
//this.height代表对象属性,和形参height进行区分
this.height = height;
}
public int getHeight() {
//省略了this,下面两句效果相同
//都显示正在调用method方法。
method();
this.method();
return this.height;
}
public ThisTest addOneToWidth() {
//this代表正在调用addOneToWidth方法的对象的引用
this.width++;
System.out.println("宽度加一,现在是:"+this.width);
//返回该对象的引用
return this;
}
public ThisTest(int height) {
this.height = height;
}
public ThisTest(int height,int width) {
//调用ThisTest(int height)构造器
this(height);
this.width = width;
}
public void method() {
System.out.println("正在调用method方法");
}
}
/*ThisAllTest.java*/
package com.my.pac09;
public class ThisAllTest {
public static void main(String[] args) {
ThisTest test = new ThisTest(1,2);
System.out.println("高度为:" + test.getHeight() + ",宽度为: " + test.width);
ThisTest me = test.addOneToWidth().addOneToWidth();
}
}
//测试结果
正在调用method方法
正在调用method方法
高度为:1,宽度为: 2
宽度加一,现在是:3
宽度加一,现在是:4
Form of expression
public int getHeight() {
//省略了this,下面两句效果相同
//都显示正在调用method方法。
method();
this.method();
return this.height;
}
//结果
正在调用method方法
正在调用method方法
public ThisTest addOneToWidth() {
//this代表正在调用addOneToWidth方法的对象的引用
System.out.println("宽度加一,现在是:"+this.width);
this.width++;
//返回该对象的引用
return this;
}
ThisTest me = test.addOneToWidth().addOneToWidth();
//结果
宽度加一,现在是:3
宽度加一,现在是:4
public ThisTest(int height) {
this.height = height;
}
public ThisTest(int height,int width) {
//调用ThisTest(int height)构造器
this(height);
this.width = width;
}
public void setHeight(int height) {
//this.height代表对象属性,和形参height进行区分
this.height = height;
}
package com.my.pac09;
/*PassingThis.java*/
public class PassingThis {
public static void main(String[] args) {
Person p = new Person();
Apple apple = new Apple(5);
apple.displayPeelNum();//此时皮的数量:5
p.eat(apple);
}
}
class Person {
public void eat(Apple apple) {
System.out.println("开始剥皮...");
Apple peeled = apple.getPeeled();
peeled.displayPeelNum();
System.out.println("剥完的苹果就是好吃~");
}
}
class Apple {
public int num;
//构造器,传入皮的数量
Apple(int num){
this.num = num;
}
Apple getPeeled() {
//意思是把apple传给Peeler的peel方法剥皮,this代表传进去的apple
return Peeler.peel(this);
}
void displayPeelNum() {
System.out.println("此时皮的数量:" + num);
}
}
class Peeler {
//static修饰的类方法,可以直接利用Peeler.peel(..)调用
static Apple peel(Apple apple) {
//...remove peel 剥皮的过程
while (apple.num > 0) apple.num--;
return apple;
}
}
//测试结果
此时皮的数量:5
开始剥皮...
此时皮的数量:0
剥完的苹果就是好吃~
PS: if there are mistakes in the article, you are welcome to criticize and correct! Reference books: Thinking in Java, crazy Java handout
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码