Keywords of this Java

Keywords of this Java

concept

this keyword can only use internal methods, is a reference to "the object is to invoke a method" of.
Specifically. There are two cases:

  • The reference constructor constructor object is being initialized .
  • In the method cited in the object calling the method .
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

Manifestations

  • Internal calls another method of the same class, can be omitted this.
 public int getHeight() {
    //省略了this,下面两句效果相同
    //都显示正在调用method方法。
    method();
    this.method();
    return this.height;
}

//结果
正在调用method方法
正在调用method方法

While in the non-static premise, methods need to be 对象.方法in the form of calls, but calls another method in a similar method, the object has been created, you do not need to repeatedly re-created in the process, this time this is this has been created object. In addition to omit this, will become directly above method();case.

  • Returns a reference to the current object.
public ThisTest addOneToWidth() {
    //this代表正在调用addOneToWidth方法的对象的引用
    System.out.println("宽度加一,现在是:"+this.width);
    this.width++;
    //返回该对象的引用
    return this;
}
    
ThisTest me = test.addOneToWidth().addOneToWidth();

//结果
宽度加一,现在是:3
宽度加一,现在是:4

You can see, addOneToWidth()the method returns the current object references, so you can perform multiple operations on objects in the same statement inside.

  • Call another constructor in the constructor.
public ThisTest(int height) {
    this.height = height;
}

public ThisTest(int height, int width) {
    //调用ThisTest(int height)构造器
    this(height);
    this.width = width;
}

Constructor this represents the initializing object references, thisplus a list of parameters, for example, produces a clear call to another constructor, you can reduce the amount of code a lot.
Further, the previously mentioned, on the one mentioned in the constructor, the this statement must be performed on the body of a constructor except annotations. Therefore, the two can not use this in a constructor! ! !

  • Parameter names and attribute names the same situation.
public void setHeight(int height) {
    //this.height代表对象属性,和形参height进行区分
    this.height = height;
}

Of course, any parameter can be used at a variable name, but with this.height=height;the form, so that others can clearly distinguish between incoming parameter and member variables, and see to know the name of justice.

  • In the method of transmitting itself to another method.

ps: Actually at this point I'm still not able to grasp, or could not give a suitable example, I refer Thinking in Java and make some changes to help understand. First attach the code:

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
剥完的苹果就是好吃~

See the following partial call Peeler peel method in class Apple , Apple just created object passed as parameter which the stripping operation, the peel Peeler external tool is a method (to be used for many other classes, for example bananas, pears, etc.), the passing itself to external means, the this is useful.

ps: the article for any errors, welcome criticism!
Reference books: "Thinking in Java", "crazy Java handouts"

Guess you like

Origin www.cnblogs.com/summerday152/p/12005295.html