[Java] this, super Keyword Description

1.this keyword

Four common usage of this keyword:

  • this call this class properties
  • This method calls this general class
  • This class constructor calls this method
  • this call the current property

     

1.1 this call this class properties:

Can clearly access to member variables of a class by this keyword, solve problems and local variable name conflicts;

example:

class Human{
       int age;
       public Human(int age){
           this.age=age;
       }
       public int getAge(){
           return this.age;

       }
}

It can be found: When the parameter of the same name and class attributes, class attributes can not be assigned properly. At this point we can add this keyword to the correct object attribute assignment. In the above example, the parameter age constructor is a local variable, but the property age class member variables, if used in the constructor age, is the local variables, but if it is used to access a member this.age variable.

1.2 calls this method by members of this class

example:

 class Human{
public void openMouth(){
           //省略内容
       }
       
       public void speak(){
           this.openMouth();
       }
}

While calling this method does not require adding this general class can be used normally, but it is strongly recommended plus, with the purpose of this method can distinguish the source of the definition of (useful for inheritance).

1.3 this class constructor calls this method

Construction method at the time of instantiation objects is java virtual machine to automatically call, not as to call the constructor to call other methods in the program, but using "this in a constructor. ([Argument 1, argument 2 ...] ) "form to call other constructors.

example:

class Human{
       int age;
       public Human(){
           System.out.println("无参构造方法被调用了…");
       }
       public Human(int age){
           //调用了无参的构造方法
           this();
           this.age=age;
           System.out.println("有参的构造方法被调用了…");
       }
}
public class Test {
    public static void main(String[] args) {
        Human h1=new Human(20);
    }
}

The results are:

When using this constructor calls the class, it should be noted:

  • Only use this call other constructor in the constructor can not be used in the method of the members;
  • In the constructor, call the constructor method of using this statement must be the first line, and can only occur once; the following wording is wrong:
public Human(){
    int age=20;
    this(age);    //调用有参的构造方法,由于不在第一行,编译错误

}
  • This can not be used in a two class constructor call each other, the following wording compiling errors
class Human{
       public Human(){
           this(20);
           System.out.println("无参构造方法被调用了…");
       }
       public Human(int age){
           //调用了无参的构造方法
           this();
           this.age=age;
           System.out.println("有参的构造方法被调用了…");
       }
}

It can not twenty-two call each other, but can call B A, C B can be called, as shown here:

class Human{
       int age;
       String name;
       public Human(){
           System.out.println("无参构造方法被调用了…");
       }
       public Human(int age){
           //调用了无参的构造方法
           this();
           this.age=age;
           System.out.println("有参的构造方法被调用了…");
       }
       
       public Human(String name,int age){
           //调用了有参的构造方法
           this(age);
           this.name=name;
           System.out.println("有参的构造方法被调用了…");
       }
}

1.4 this call the current object

class Human{
       //this调用当前对象
       public void print(){
           System.out.println("print方法"+this);
       }
}
public class Test {
    public static void main(String[] args) {
        Human h1=new Human();
        System.out.println("main方法"+h1);
        h1.print();
        System.out.println("======================");
        Human h2=new Human();
        System.out.println("main方法"+h2);
        h2.print();
    }
}

operation result:

As long as the object calling the method of this class, it means that this object is currently executing

2.super keyword

(Written before using the super reference specific blog: https://mp.csdn.net/postedit/102506011 )

When the method of the parent class subclass overrides, subclass object can not access the parent class is overridden, in order to solve this problem, in java specializes in providing members a super keyword is used to access the parent class,

2.1 Use super keyword to call member variables and methods of the parent class members

The format is:

super. member variables

super. member method ()

2.2 Use super keyword to call the constructor of the parent class

Format is as follows:

Super ([parameter 1, parameter 2])

3.this and super Compare

and this super very similar in use, but the biggest difference between the two classes are subclasses of the parent when accessing super, but this is the type of access to the processing operation.

Super can be used. Method () operation must be explicitly marked parent class.

1. subclasses override the parent class method function because of lack of parent class only need to override.

2. When overridden method is the use of public authority.

3. It can be said parent class is a subset of the class.

Published 62 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43669007/article/details/102925976