Java basic study notes _ the use characteristics of member variables and member methods in inheritance relations

1. The use characteristics of member variables in Java inheritance relations:

Follow the "principle of proximity", and use it if you have a local location,

If not, go to the member position of this class to find, use if there is,

If not, go to the member location of the parent class to find it, use it if it has it, and report an error if it doesn't.

public class Fruit {
    int price = 20;
}

public class Apple extends Fruit {
    int price = 10;
    public void showPrice() {
        int price = 5;
        System.outprintln(price);            //5
        System.outprintln(this.price);       //10
        System.outprintln(super.price);      //20
    }
}

public class Test {
    public static void main(String args[]) {
        Apple apple = new Apple();
        apple.showPrice();
    }
}

 In the above code, the printed value of the ninth line of code is 5;

If the eighth line is commented out, the printed value is 10;

If you continue to comment out the sixth line, the printed value is 20;

If the second line is commented out, the system will report an error.

 

By default, the value closest to itself is printed.

If you want to call the member variables of this class in the above circumstances, you need to use the this keyword;

If you want to call the member variables of the parent class, you need to use the super keyword.

2. Features of using member methods in Java inheritance relationship:

Method rewriting:

When a method that is exactly the same as the parent class appears in a subclass (the permission modifier of the subclass method is not less than that of the parent class method, and the return value type, method name and parameter list are the same), an overwrite operation will occur, also known as rewriting or copying.

The private method of the parent class cannot be seen by the subclass, so there is no way to rewrite the private method of the parent class.

Rewrite notes:

The permission modifier of the subclass method is not less than that of the parent class method.

Static can only override static.

Covered usage scenarios :

When the subclass needs the functions of the parent class, and the main function subclass has its own unique content, the methods in the parent class can be overwritten.

In this way, both the functions of the parent class are inherited, and the unique content of the subclass is defined.

What is the difference between method rewriting and overloading ?

Method rewriting is used when the subclass method is exactly the same as the parent method. Except for the permission modifier, the return value type, method name, and parameter list are the same.

Overloading is used when the method name of each method in the same class is the same, and the parameter list is different (it has nothing to do with the return value type).

The usage of the construction method in the sub-parent class :

1. In the initialization process of the subclass, first go back and execute the initialization action of the parent class.

Because there is a super() in the subclass's construction method by default.

2. If the parent class does not have a parameterless construction method:

  • Use super to call the parameterized structure of the parent class. (Recommended method)
  • Use this to call other constructs of itself.

The execution sequence of static code block, construction code block, and construction method :

Parent class static code block → sub class static code block → parent class construction code block → parent class construction method → ​​child class construction code block → child class construction method

Guess you like

Origin blog.csdn.net/qq_43191910/article/details/114780075