Easy Tutorial
❮ Vmware Install Centos7 Php Remove Firsrt Array Element ❯

Summary of Usage of this and super in Java

Category Programming Technology

this

this is an object itself, representing the object itself, and can be understood as: a pointer pointing to the object itself.

The usage of this in Java can be broadly classified into three types:

1. Ordinary Direct Reference

This is straightforward, where this points to the current object itself.

2. Differentiating Between Formal Parameters and Member Names

Example

class Person {
    private int age = 10;
    public Person(){
        System.out.println("Initial age: " + age);
    }
    public int GetAge(int age){
        this.age = age;
        return this.age;
    }
}

public class test1 {
    public static void main(String[] args) {
        Person Harry = new Person();
        System.out.println("Harry's age is " + Harry.GetAge(12));
    }
}

Running Result:

Initial age: 10
Harry's age is 12

Here, age is the formal parameter of the GetAge member method, and this.age is the member variable of the Person class.

3. Referencing Constructor

This will be discussed together with super below.


super

super also has three usages:

1. Ordinary Direct Reference

Similar to this, super points to the parent class of the current object, allowing you to reference the parent class's members with super.xxx.

2. Member Variables or Methods in Subclass with the Same Name as in Parent Class

Example

class Country {
    String name;
    void value() {
        name = "China";
    }
}

class City extends Country {
    String name;
    void value() {
        name = "Shanghai";
        super.value(); // Call the parent class's method
        System.out.println(name);
        System.out.println(super.name);
    }

    public static void main(String[] args) {
        City c = new City();
        c.value();
    }
}

Running Result:

Shanghai
China

Here, both the parent class's method and variable are called. If the parent class's method value() is not called, the parent class's variable name would have the default value null.

3. Referencing Constructor

Example

class Person {
    public static void prt(String s) {
        System.out.println(s);
    }

    Person() {
        prt("Parent class · No-argument constructor: " + "A Person.");
    } // Constructor (1)

    Person(String name) {
        prt("Parent class · Constructor with one parameter: " + "A person's name is " + name);
    } // Constructor (2)
}

public class Chinese extends Person {
    Chinese() {
        super(); // Call the parent class's constructor (1)
        prt("Subclass · Calling parent class 'no-argument constructor': " + "A Chinese coder.");
    }

    Chinese(String name) {
        super(name); // Call the parent class's constructor with the same parameter (2)
        prt("Subclass · Calling parent class 'constructor with one parameter': " + "his name is " + name);
    }

    Chinese(String name, int age) {
        this(name); // Call the constructor with the same parameter (3)
        prt("Subclass: Calling subclass constructor with the same parameter: his age is " + age);
    }

    public static void main(String[] args) {
        Chinese cn = new Chinese();
        cn = new Chinese("codersai");
        cn = new Chinese("codersai", 18);
    }
}

Running Result:

Parent class · No-argument constructor: A Person.
Subclass · Calling parent class 'no-argument constructor': A Chinese coder.
Parent class · Constructor with one parameter: A person's name is codersai
Subclass · Calling parent class 'constructor with one parameter': his name is codersai
Parent class · Constructor with one parameter: A person's name is codersai
Subclass · Calling parent class 'constructor with one parameter': his name is codersai
Subclass: Calling subclass constructor with the same parameter: his age is 18

From this example, you can see that super and this can be used to call the parent class's constructor and other constructors within the same class, respectively.

In the example, the third constructor of the Chinese class calls the second constructor of the same class, which in turn calls the parent class's constructor. Therefore, the parent class's constructor is called first, followed by the second constructor of the same class, and finally the third constructor is overridden.


Differences and Similarities Between super and this

>

Original Link: https://www.cnblogs.com/hasse/p/5023392.html

** Click to Share Notes

Cancel

-

-

-

❮ Vmware Install Centos7 Php Remove Firsrt Array Element ❯