Inheritance, Super, this

 1 /**

2 * Inheritance: Subclass inherits the attributes and behaviors of the parent class, making the subclass The object has the same properties and the same behavior as the parent class. Subclasses can be directly
3 * Access to non-private attributes and behaviors in the parent class
4 * */
5
6 /**
7 * class Parent class{
8 * ...
9 *}
10 * class subclass extends parent class{
11 * ...
12 *}
13 * */

 1< /span> public class Employee {

2
3 String name;
4
5 public void work(){
6 System.out.println("Overtime and overtime") ;
7 }
8 }
9
10
11 public class Teacher extends Employee {
12
13 public void printName(){
14 System.out.println("name" + name);
15 }
16 }
17
18
19 public static void main(String[] args) {
20
21 Teacher t = new Teacher();
22 t.name = "weak chicken";
23 t.printName();
24 t.work();
25 }

< pre> 1 public class< /span> Fu

2 {

3 int num = 5;

4 int num2 = 6;

5 }

6

7

8 public class Zi extends Fu {

9

10 int num = 6;

11 /**

12 * When a member variable with the same name appears in the child parent class, in the child class When you need to access non-private member variables in the parent class, you need to use the super keyword to modify

13 * Parent class member variables, similar to this learned before

14 * */

15 public void show(){

16 System.out.println(“Fu num” + super< /span>.num);

17 System.out.println(“Zi num” + this. num);

18 }

19 }

20

21

22 public static void main(String[] args) {

23 Zi z = new Zi();

24 z.show();

25 }

< pre>1 /**

2 * Same member method name-Override

3 * If there is a member method with the same name in the parent class of the subclass At this time, the access is a special case, called method override (Override).

4 * Method rewriting: the child class appears exactly the same as the parent class When the method (return value type, method name and parameter list are the same), there will be an overwrite effect

5 * Fruit, also known as rewriting or copying. The statement remains unchanged and reimplemented

6 * */

 1< /span> /**

2 * 1. The name of the construction method is consistent with the class name. Therefore, the subclass cannot inherit the construction method of the parent class.
3 * 2. The role of the constructor is to initialize member variables. Therefore, in the initialization process of the subclass, the initialization action of the parent class must be executed first. Construction of subclasses
4 * There is a super() by default in the creation method, which means to call The construction method of the parent class can be used by the child class after the parent class member variables are initialized. generation
5 * The code is as follows:
6 * */
7
8
9
10 public class Fu {
11
12 private int n;
13
14 public Fu() {
15 System.out.println("FU()");
16 }
17 }
18
19
20
21 public class Zi extends Fu{
22
23 public Zi() {
24 super();
25 System.out.println("Zi");
26 }
27 }
28
29
30 public static void main(String[] args) {
31 Zi z = new Zi();
32 }

< pre>1 /**

2 * The meaning of super and this

3 * super: represents the storage space identifier of the parent class (understandable For the father’s quote).

4 * this: represents the reference of the current object (who calls it represents Who).
5 * Java only supports single inheritance, not multiple inheritance.

6 * */

 1 /**

2 * Inheritance: Subclass inherits the attributes and behaviors of the parent class, making the subclass The object has the same properties and the same behavior as the parent class. Subclasses can be directly
3 * Access to non-private attributes and behaviors in the parent class
4 * */
5
6 /**
7 * class Parent class{
8 * ...
9 *}
10 * class subclass extends parent class{
11 * ...
12 *}
13 * */

 1 public class Employee {

2
3 String name;
4
5 public void work(){
6 System.out.println("Overtime and overtime") ;
7 }
8 }
9
10
11 public class Teacher extends Employee {
12
13 public void printName(){
14 System.out.println("name" + name);
15 }
16 }
17
18
19 public static void main(String[] args) {
20
21 Teacher t = new Teacher();
22 t.name = "weak chicken";
23 t.printName();
24 t.work();
25 }

 1 public class Fu

2 {
3 int num = 5;
4 int num2 = 6;
5 }
6
7
8 public class Zi extends Fu {
9
10 int num = 6;
11 /**
12 * When a member variable with the same name appears in the child parent class, in the child class When you need to access non-private member variables in the parent class, you need to use the super keyword to modify
13 * Parent class member variables, similar to this learned before
14 * */
15 public void show(){
16 System.out.println("Fu num" + super< /span>.num);
17 System.out.println("Zi num" + this. num);
18 }
19 }
20
21
22 public static void main(String[] args) {
23 Zi z = new Zi();
24 z.show();
25 }

1 /**

2 * Same member method name-Override
3 * If there is a member method with the same name in the parent class of the subclass At this time, the access is a special case, called method override (Override).
4 * Method rewriting: the child class appears exactly the same as the parent class When the method (return value type, method name and parameter list are the same), there will be an overwrite effect
5 * Fruit, also known as rewriting or copying. The statement remains unchanged and reimplemented
6 * */

 1 /**

2 * 1. The name of the construction method is consistent with the class name. Therefore, the subclass cannot inherit the construction method of the parent class.
3 * 2. The role of the constructor is to initialize member variables. Therefore, in the initialization process of the subclass, the initialization action of the parent class must be executed first. Construction of subclasses
4 * There is a super() by default in the creation method, which means to call The construction method of the parent class can be used by the child class after the parent class member variables are initialized. generation
5 * The code is as follows:
6 * */
7
8
9
10 public class Fu {
11
12 private int n;
13
14 public Fu() {
15 System.out.println("FU()");
16 }
17 }
18
19
20
21 public class Zi extends Fu{
22
23 public Zi() {
24 super();
25 System.out.println("Zi");
26 }
27 }
28
29
30 public static void main(String[] args) {
31 Zi z = new Zi();
32 }

1 /**

2 * The meaning of super and this
3 * super: represents the storage space identifier of the parent class (understandable For the father's quote).
4 * this: represents the reference of the current object (who calls it represents Who).
5 * Java only supports single inheritance, not multiple inheritance.
6 * */

Leave a Comment

Your email address will not be published.