JAVA in this and super Detailed usage and differences

<A>. This usage

Reproduced in more than the old blog https://www.cnblogs.com/yyy6/p/8976584.html

Constructor is an important way to create java objects, through the new keyword call the constructor, the constructor returns an object of the class, but the opponent is not entirely created by the builder responsible for creating an object is divided into the following four steps:

a. assignment space, and the object member variable is initialized to zero or null

b. Perform attribute values ​​explicitly initialized

c. Perform constructor

d. returns the address to the object-related variables

The essence of this is to "create a good object address", due before the constructor method calls, object has been created. Therefore, in the constructor can also use this on behalf of "the current object."

For this description of the book

When this is a reference to an object that points to an object is performing the method. In particular, in the constructor, by this keyword to call other constructors, it must be placed on the first line, otherwise the compiler will complain. And in the constructor , you can only call other constructors once through this.

  this举例一
  package cn.sxt.oop;

public class Test_this_super {
    int id;
    String name;
    boolean man;
    double width;
    public Test_this_super(){
        System.out.println("无参数构造器");
    }
    
    public Test_this_super(int number,String name){
        this();
        System.out.println("传递int和String形式参数构造器");
    }
    
    public Test_this_super(int number,String name,double _width){
        this(number,name);
        this.width = _width;
        System.out.println("传递int, String, double形式参数构造器");
    }
    
    public Test_this_super(int number,String name,char a){
        System.out.println("传递int, String, char形式参数构造器");
    }
    
    public static void main(String[] args) {
        Test_this_super t1 = new Test_this_super();
        Test_this_super t2 = new Test_this_super(1000,"Tom");
        Test_this_super t3 = new Test_this_super(2000,"long",3.14);
        Test_this_super t4 = new Test_this_super(2000,"long",'a');
    }
}

Print results

t1   无参数构造器
 t2   无参数构造器
      传递int和String形式参数构造器
 t3   无参数构造器
      传递int和String形式参数构造器
      传递int, String, double形式参数构造器
 t4   传递int, String, char形式参数构造它器

Graphical analysis
Here Insert Picture Description

<Two> super Usage

super is a reference point to the parent class, if the constructor does not call the parent class constructor explicitly, then the compiler will automatically add method calls it a default super (). If the parent does not like the default constructor with no arguments, the compiler will report an error, super () statement must be the first clause of the constructor.

When you define a subclass of objects, it will first call the constructor of the subclass, and then call the constructor of the parent class,

  • If the parent class function enough, it would have been the final call to the parent class constructor ( the test did not find such features )

Will be used stack space when the function call, so the order of the stack, is the first to enter the sub-class constructor, and then the neighboring parent class constructor, and finally the top of the stack is the ultimate parent class constructor, the constructor is executed sequentially performed in the order from the top of the stack to the bottom of the stack.

Now Bird-> Animal -> Object of illustration a line

public class Animal {
    int eye = 2;
    public Animal(){
        super();
        System.out.println("动物");
    }
    
    public void run(){
        System.out.println("动物有不同走路方式");
    }
    
    public static void main(String[] args) {
        Bird b = new Bird();
        b.run();
    }
}

class Bird extends Animal{
    public Bird(){
        super();
        System.out.println("鸟类");
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        super.run(); // 通过super可以用父类方法和属性
        System.out.println("鸟是飞飞飞飞飞飞");
        System.out.println("鸟类有"+super.eye+"只眼睛");
    }
    
}

Print results

动物
鸟类
动物有不同走路方式
鸟是飞飞飞飞飞飞
鸟类有2只眼睛

Bird–> Animal --> Object 图形分析如下
Here Insert Picture Description

<三> this和super差异

1)super(参数):调用基类中的某一个构造函数(应该为构造函数中的第一条语句)

2)this(参数):调用本类中另一种形成的构造函数(应该为构造函数中的第一条语句)
  3)super: 它引用当前对象的直接父类中的成员(用来访问直接父类中被隐藏的父类中成员数据或函数,基类与派生类中有相同成员定义时如:super.变量名 super.成员函数据名(实参)

4)this:它代表当前对象名(在程序中易产生二义性之处,应使用this来指明当前对象;如果函数的形参与类中的成员数据同名,这时需用this来指明成员变量名)

5)调用super()必须写在子类构造方法的第一行,否则编译不通过。每个子类构造方法的第一条语句,都是隐含地调用super(),如果父类没有这种形式的构造函数,那么在编译的时候就会报错。

6)super()和this()类似,区别是,super()从子类中调用父类的构造方法,this()在同一类内调用其它方法。

7)super()和this()均需放在构造方法内第一行。

8)尽管可以用this调用一个构造器,但却不能调用两个。

9)this和super不能同时出现在一个构造函数里面,因为this必然会调用其它的构造函数,其它的构造函数必然也会有super语句的存在,所以在同一个构造函数里面有相同的语句,就失去了语句的意义,编译器也不会通过。

10)this()和super()都指的是对象,所以,均不可以在static环境中使用。包括:static变量,static方法,static语句块。

11) In essence **, this is a pointer to this object, but super is a Java keyword. **
 
  reproduced in more than the old blog https://www.cnblogs.com/yyy6/p/8976584.html

Published 27 original articles · won praise 5 · Views 650

Guess you like

Origin blog.csdn.net/qq_44620773/article/details/104015116