11 use of keyword this

11 use of keyword this

1, What is this

In Java, the function and meaning of this are very close:

  • Used inside a method, that is, the reference of the object to which the method belongs;
  • Used inside a constructor to indicate the object that the constructor is initializing.

this table is the current object, which can call the properties, methods and constructors of the class.

2, When to use this keyword

When the object that needs to be called to build a house in the method is, use this.

Specifically, you can use this to distinguish between attributes and local variables, such as this name = name;

3, this modifier attribute and method

this is understood as the current object or the object currently being created.

In the method of the class, you can use "this. Attribute" or "this. Method" to call the attribute or method of the current object. However, in general, choose to omit "this.". In special cases, if the formal parameter of the method has the same name as the attribute of the class, nongxi explicitly uses "this. Variable" to indicate that the variable is an attribute rather than a formal parameter.

In the constructor of the class, you can use the "this. Property" or "this. Method" method to call the property or method of the current object. However, in general, choose to omit "this.". In special cases, if the formal parameter of the method has the same name as the attribute of the class, nongxi explicitly uses "this. Variable" to indicate that the variable is an attribute rather than a formal parameter.

public class PersonTest {
    public static void main(String[] args) {
        Person p1 = new Person("Tom");
        p1.setAge(18);
        p1.show();
        p1.study();
    }
}

class Person{
    //attribute
    private int age;
    private String name;

    //constructor 
    public Person(String name) {
        this.name = name;
    }

    public Person(int age) {
        this.age = age;
    }

    public Person() {
        this.eat();
    }

    //method
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void show() {
        System.out.println("full name:" + this.getName() +"´╝ŤAge:" + this.getAge());
    }

    public void eat() {
        System.out.println("Can eat");
    }

    public void study() {
        System.out.println("Can learn");
        this.eat();
    }
}

4, this modifier constructor

In the constructor of the class, you can use "this (formal parameter list)" to call other constructors specified in this class;

The constructor cannot call itself through "this (formal parameter list)";

If there are N constructors in a class, up to n-1 constructors use "this (formal parameter list)";

Specify what "this (formal parameter list)" must be in the first line of the current constructor;

Inside the constructor, at most one "this (formal parameter list)" can be declared.

public class PersonTest {
    public static void main(String[] args) {
        Person p2 = new Person("Jerry", 22);
        p2.show();
    }
}

class Person{
    //attribute
    private int age;
    private String name;

    //constructor 
    public Person() {
        this.study();
        System.out.println("this Modifier constructor");
    }

    public Person(String name) {
        this();
        this.name = name;
    }

    public Person(String name, int age) {
        this(name);
        this.age = age;
    }

    //method
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void show() {
        System.out.println("full name:" + this.getName() +"´╝ŤAge:" + this.getAge());
    }

    public void eat() {
        System.out.println("Can eat");
    }

    public void study() {
        System.out.println("Can learn");
        this.eat();
    }
}

5, Practice

Add the necessary constructors. Comprehensive application of constructor overload and this keyword.

public class Boy {
    private String name;
    private int age;

    public Boy() {

    }

    public Boy(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void marry(Girl girl) {
        System.out.println("I want to marry" + girl.getName());
    }

    public void shout() {
        if (this.age >= 22) {
            System.out.println("You can get married legally!");
        } else {
            System.out.println("You can fall in love first~~");
        }
    }
}

public class Girl {
    private String name;
    private int age;

    public Girl() {

    }

    public Girl(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void marry(Boy boy) {
        System.out.println("I want to marry" + boy.getName());
        boy.marry(this);
    }

    public int compare(Girl girl) {
        return this.age - girl.age;
    }
}

public class BoyGirlTest {
    public static void main(String[] args) {
        Boy boy = new Boy("Romeo",21);
        boy.shout();

        Girl girl = new Girl("Juliet", 18);
        girl.marry(boy);

        Girl girl1 = new Girl("Zhu Yingtai", 19);
        int compare = girl1.compare(girl);
        if (compare > 0) {
            System.out.println(girl1.getName() + "Old age");
        } else if (compare < 0) {
            System.out.println(girl.getName() + "Old age");
        } else {
            System.out.println("Same age");
        }
    }
}

Tags: Java Class

Posted by bookbuyer2000 on Sun, 17 Apr 2022 07:25:57 +0930