[Java Basics] Reference Arrays, Java Inheritance, Super Keyword Detailed Explanation

insert image description here

The column " Java Zero-Basic Introduction to Mastery " is continuously updated. Through this column, you will learn a complete set of Java content from entry to advanced to actual combat , and all the content will be concentrated in this column. Both beginners and experienced developers can benefit from this column.


After subscribing to the column, add me on WeChat or enter the communication group . When you enter the group, you can find me to receive a full set of video courses such as front-end/Java/big data/Python/low-code/cloud native, and discuss problems together and work together~


1. An array of reference types

As explained earlier, arrays are divided into arrays of basic types and arrays of reference types. Let's take a look at the difference between the two through code.

//基本类型数组
int[] a = new int[3];
double[] b = new double[3];

//引用类型数组 
Student[] stu = new Student[3];

Both int and double are basic data types, so the corresponding array is the basic type array, and each element in the array is its corresponding data type. In the reference type array, we use a certain class, and the Student here refers to a class. This statement means: declare an array of Student type, which contains three elements, each element is of Student type, and the default value is null.

When declaring any array, whatever type is before the square brackets [ ], the element of the array is also what type.

The assignment methods of these two array types are different, and now the demonstration assigns a value to the first element of the array;

//基本类型数组
int[] a = new int[3];
a[0] = 100; //为数组中的第一个元素赋值

//引用类型数组
Student[] stu = new Student[3];
stu[0] = ?;

It can be seen that the basic type array here is int type, so int type data can be directly assigned to its elements. However, the elements in the reference type array are not basic types of data and cannot be directly assigned, so the assignment method should be as follows:

//为引用类型数组中的元素赋值
Student[] stu = new Student[3];
stu[0] = new Student("zhangsan",24,"山西太原"); //学生1
stu[1] = new Student("lisi",25,"北京东城"); //学生2
stu[2] = new Student("wangwu",26,"陕西延安"); //学生3

The access methods of the elements of these two types of arrays are also different. The basic type array can be 数组名[下标]accessed , but when we want to access an element in the reference type array, 数组名[下标]it is wrong to write directly, and the correct access method is 数组名[下标].属性;

//访问引用数组中的元素
System.out.println(stu[0].name); //输出学生1的名字:zhangsan
System.out.println(stu[0].age); //输出学生1的年龄:24
System.out.println(stu[1].name); //输出学生2的名字:lisi
stu[0].age = 30; //修改学生1的年龄为30
stu[2].方法名(); //学生3调用某个方法

Two, inheritance (extends)

"Inheritance", as the name suggests, means that something can be used directly without creating it yourself. This is a very important concept in the Java language. Its greatest significance is to improve the reusability of code . When there are multiple classes that need to use the same attributes and methods, these same contents are extracted into a single class. , then multiple classes do not need to define these attributes and behaviors, as long as they inherit from that class.

The keywords used in inheritance are extends, as follows, we first define a Person class, including three attributes: name (name), age (age), address (address), two attributes for eating (eat()) and sleeping (sleep()) method. Then define the student class (Student) and the teacher class (Teacher) to inherit the Person class.

class Person{
    
    
    String name;
    int age;
    String address;
    void eat(){
    
    }
    void sleep(){
    
    }
}

//定义学生类 继承Person
class Student extends Person{
    
    
    String stuId;
    void study(){
    
    }
}

//定义教师类 继承Person
class Teacher extends Person{
    
    
    double salary;
    void teach(){
    
    }
}

After using the extends keyword to inherit, any member of the student class and the teacher class can directly access the properties and methods in their own class and the properties and methods in the Peron class.

class Person{
    
    
    String name;
    int age;
    String address;
    void eat(){
    
    }
    void sleep(){
    
    }
}

//定义学生类 继承Person
class Student extends Person{
    
    
    String stuId;
    void study(){
    
    }
}
Student zhangsan = new Student(); //定义学生类的成员-zhangsan
//zhangsan可直接访问学生类和Person类中的属性和方法
zhangsan.stuId = "";
zhangsan.study();
zhangsan.name = "";
zhangsan.age = ;
zhangsan.eat();

//定义教师类 继承Person
class Teacher extends Person{
    
    
    double salary;
    void teach(){
    
    }
}
Teacher laoshi = new Student(); //定义教师类的成员-laoshi
//laoshi可直接访问教师类和Person类中的属性和方法
laoshi.salary = "";
laoshi.teach();
laoshi.name = "";
laoshi.age = ;
laoshi.eat();

In this case, we have defined three classes in total, among which the inherited class is called Person 父类/超类, and the inherited parent class is called Student class and Teacher class 子类/派生类. The parent class contains common properties and methods, and the subclass contains unique properties and methods. Subclasses can access properties and methods of themselves and the parent class, but the parent class cannot access the properties and methods of the subclass. In addition, inheritance also includes several features:

  1. Single inheritance , a parent class can have multiple subclasses, but a subclass can only inherit from one parent class;
  2. Transitivity , the code is as follows;
  3. Before constructing a subclass, the superclass must be constructed first .
class A{
    
     //类A仅可访问a
    int a;
}

class B extends A{
    
     //类B可访问b、a
    int b;
}

class C extends B{
    
     //类C可访问c、b、a
    int c;
}

3. The super keyword

Note the distinction between super and this:

  • super refers to the parent class object of the current object
  • this refers to the current object

The super keyword refers to the parent object of the current object. It is used like this:

  • super.成员变量名- Access member variables of the parent class
  • super.方法名()- Access methods of the parent class
  • super()- Call the constructor of the parent class

Here we focus on the use of super(). As we know in the previous section, the characteristic of inheritance is that the parent class must be constructed before the subclass is constructed. Java has a clear execution mechanism for this feature: in the construction method of the subclass, if the construction method of the parent class is not called, the default Use super() to call the no-argument constructor of the parent class ;

In the following case, we create a parent class Aoo and a subclass Boo and contain their respective construction methods, and Boo inherits Aoo;

public class Super {
    
    
    public static void main(String[] args) {
    
    
        Boo o = new Boo(); //创建Boo类下的对象o
    }
}
//父类A
class Aoo{
    
    
    Aoo(){
    
    
        System.out.println("父类构造方法");
    }
}
//子类B
class Boo extends Aoo{
    
    
    Boo(){
    
    
        System.out.println("子类构造方法");
    }
}

Then create an object o under the subclass Boo in the main method, and run the program;

insert image description here
According to the normal program execution sequence, the object o is under the subclass Boo, then the construction method of Boo must be executed first after running the program, that is, the words "subclass construction method" should be output. But the result is that the constructor in the parent class is executed first, and then the constructor in the subclass.

This is precisely because of the characteristics 构造子类之前必须先构造父类of , the subclass Boo does not call the constructor of the parent class Aoo, so here is used by default super()to call the constructor of the parent class; the complete code of the subclass Boo is actually as follows:

//子类B
class Boo extends Aoo{
    
    
    Boo(){
    
    
    	super(); //该语句默认存在,调用父类无参构造方法
        System.out.println("子类构造方法");
    }
}

This also causes the execution order of the program to be executed first super(), and the "parent class construction method" is output, and then executed System.out.println("子类构造方法");, and the "subclass construction method" is output.

So if the subclass itself calls the constructor of the parent class, how will it be executed?

If the parent class constructor has been called in the subclass, super()the statement ;

public class Super {
    
    
    public static void main(String[] args) {
    
    
        Boo o = new Boo(); //创建Boo类下的对象o
    }
}
//父类A
class Aoo{
    
    
    Aoo(int a){
    
    
        System.out.println(a);
    }
}
//子类B
class Boo extends Aoo{
    
    
    Boo(){
    
    
        super(5); //子类自己调用超类构造方法
    }
}

The result of the program running is as follows:

insert image description here

4. Recommendations in this issue

insert image description here

Guess you like

Origin blog.csdn.net/weixin_53072519/article/details/130440391