Java learning super & this

Before talking about the super keyword, let's first introduce the this keyword, and compare the two to learn.

this - the application that points to the current instance object

Simply put, this is a pointer to the current instance object. The common methods of this are as follows

  1.     call a property or method of the current object
  2.     Returns a reference to the current object as return value
  3.     You can use this inside a constructor to call another constructor
  4.     Distinguish between properties and local variables
The following code comment numbers correspond to the functions above


  1. public class Demo {
    	int num;
    	String name;
    	public Demo(){
    		num=10;
    	}
    	public Demo(String name)
    	{
    		this();     //3. Call the constructor, no parameters are regarded as calling the no-parameter constructor, which can only appear on the first line
    		this.name=name; //4.this calls the properties of the current object, distinguishing properties and local variables
    	}
    	public void print(){
    		System.out.println("num="+this.num);
    		System.out.println("name="+this.name);
    	}
    	public Demo addNum(){
    		this.num++; //1. Call the properties of the current object
    		return this; //2. Return a reference to the current object
    	}
    	public static void main(String[] args) {
    		Demo exp=new Demo("sdf");
    		exp.print();
    		exp.addNum().addNum().addNum().print(); //The current object returned calls the addNum() method again to achieve accumulation
    	}
    

    The result of execution is:
    num=10
    name=sdf
    num=13
    name=sdf
    It should be noted here: when using the this keyword inside a constructor to call other constructors, the called code can only appear in the first line of executable code inside the constructor , that is, using this to call the constructor will only appears once.

         The advantage of using this to call other constructors in a constructor is: when there are many constructors, you can write only the internal function code of one constructor, and then other constructors are implemented by calling this constructor, which ensures that all The structure is unified, which can effectively reduce the repetition rate of the code and facilitate subsequent program modification.

super - a reference to the supertype of the current object or the constructor of the supertype

The common method is similar to this, except that super targets the parent class:

  1. Call a method or property of the parent class
  2. When the parent class has not defined a parameterless constructor, you can use super in the constructor of the subclass to call the constructor of the parent class with parameters.

Paste the code:

package com.LearningJava.dyp;

class Par{
	int a;
	/*public Par(){
		a=1;
	}*/
	public Par (int a) {
		//this();
		this.a=a;
	}
	public void Print(){
		System.out.println("a="+a);
	}
}
public class Demo extends Par{
	int b=1;
	public Demo(){
		super(4);             //2. Call the parent class parameter constructor, and the statement should be placed on the first line
		b=0;
	}
	public void Print(){
		super.Print(); //Call the Print() method of the parent class
		System.out.println("b="+b);
	}
	public static void main(String[] args) {
		Demo demo=new Demo();
		demo.Print();
	}
}
/////
The results are as follows:
a=4
b=0 
It should be noted here that if super(4) is not used, the following error will be reported:

            Implicit super constructor Par() is undefined. Must explicitly invoke another constructor

It means that the parent class has defined a parameterized constructor, then the default no-parameter constructor cannot be called, so it is necessary to manually call the parent class's constructor in the subclass's constructor.

It should also be noted that: super() calling the superclass constructor needs to be placed in the first line of the subclass constructor , otherwise an error will be reported. It can also be seen that in a constructor, super() and this( cannot exist at the same time. ), because they are all in the first row position.



Again


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325972417&siteId=291194637