Three points to be clear when learning and use inheritance

zhaozj2021-02-12  138

Author: qlampskyface contact the author: xiaozuidaizhi@sina.com

The first point:

That is

1. If the subclass covers the method of the parent class, the sub-class object calls the method, the class level variable modified by this method is the variable of the subclass. 2. If the subclass does not override the parent class method, the method of the parent class is directly called, the class level variable modified by this method is the variable of the parent class.

1 is as follows:

Public class temp {public static void main (string args []) {b bb = new b (); bb.fuqin ();}} class a {public string s = "fuqin"; public void fuqin () {system. Out.println (s);}} class b extends a {public string s = "erzi"; public void fuqin () {system.out.println (s);}} / / result is ERZI

2. The following example:

Public class temp {public static void main (string args []) {b bb = new b (); bb.fuqin ();}} class a {public string s = "fuqin"; public void fuqin () {system. Out.println (s);}} class b extends a {public string s = "erzi"; public void Erzi () {system.out.println (s);}} / / result is Fuqin

Second point:

That is the clear concept of "override" and "overload" and reconstruction. First declare: Reconstruction is to write a new method. As in the following example: The following is overwriting

Public class temp {public static void main (String [] args) {sub subs (2); system.out.println (sub.i);}}

Class super {protected int i; void plus (int i) {this.i = i;}}

Class Sub Extends Super {Protected Int I = 1; Void Plus (INT I) {THIS.I = I 5;}}

This is called reconstruction below, not overloaded:

Public class temp {public static void main (String [] args) {sub subs (2); Sub.plus (2, 2); system.out.println (Sub.i); System.out.println ((Super) Sub) .i);}}

Class super {protected int i; void plus (int i) {this.i = i;}}

Class Sub Extends Super {Protected Int i = 1; Void Plus (INT I, INT J) {this.i = i j;}} The third point: That is, "Inheritance after inheriting", " Dynamic Method Scheduling, Runtime Type Identification. Don't be fooled by the above terms :-) You can summarize the following two: 1. Default status (and forced type conversion status difference), this object (SUB) can only call the parent class method and parent class variable (The compile period error occurs when calling the subclass method). In this case, there are two different situations: ▲ The modified method is a method covered by the subclass. In fact, it is actually an explicit call parent class method, which is actually called the sub-class method and the sub-variable. At this time, the parent class variable cannot be modified by the method, and the subclass variable is modified. ▲ The called method is not covered by the subclass. At this time, the parent class method is called, and the parent class variable can be modified by this method. Take a look at the code below, pay attention to 1 ~ 5. Public class temp {public static void main (string [] args) {super sub = new sub (); system.out.println (sub.i); // print result is 0 ---------- ------------------------------------ 1 Sub.plus (3); // Print results are Sub's Parent method that is overwritten in the default state, // actually call the subclass of this method -------------------------------------------------------------------------------------------- ---------------------------- 2 System.out.println (Sub.i); // Print results are 0 ---- ------------------------------------------ 3 Sub.print (); / / Print results are super. Default status is called the parent class method that is not covered, the actual is this class (parent) method ---------------------- --------------------------- 4 System.out.println (Sub.i); // Print results are 5: ---- ----------------------------------------- 5}} Class Super {Protected Int i = 0; Void Plus (INT i) {this.i = 3; System.out.Println ("Super's Plus");} void print () {this.i = 5; System.out.Println ("super") }}} Class sub@ = 1; Void Plus (INT i) {this.i = i 8; System.out.Println ("Sub's Plus");} Void Plus (INT I, INT J ) {THIS.I = i j;}} 2. Mandatory type conversion state [Note 1], the object (SUB) can call the sub-class method, but also call the father that is not covered Class method.

Take a look at the code below, pay attention to 6, 7: public class temp {public static void main (string [] args) {super sub = new sub (); (sub) SUB) .plus (3); // call Made in the method ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 6 ((sub) sub) .print (); // Call the parent class method that is not covered, change the parent class variable -------------- 7}} Class Super {Protected Int i = 0; Void Plus (INT i) {this.i = 3; System.out.Println ("Super's Plus:" this.i);} void print () {this.i = 5; System.out. Println ("Super's Print");}} Class Sub Extends Super {Protected Int i = 1; Void Plus (INT I) {THIS.I = I 8; System.out.Println ("Sub's Plus:" THIS. i);}} [Note 1] The result of the parent class forced conversion and the default state, even if it is overwritten, the result is still actually called the sub-class method. And the subclass variable. So, only discuss the sub-class forced conversion here. The third point is summarized as follows: 1. If you want to call the method of overwrite the parent class, you can call in the default state, or you can call after mandatory conversion. Because whether in the default state, or use the parent class to make a mandatory conversion call, or use the subclass to make a forced conversion call, the result is the same, that is, the method of being covered by the subclass of the subclass;

2. If you want to call the method of not being covered by the subclass of the parent class, you can call it in the default state, or it can be called after being forced by the subclass. all the same.

3. If you want to call the method of the parent class coverage by the subclass, you can't afford it, you can't get it, but you can get the normal creation of the parent object name = New parent class () ".

4. If you want to call the parent class variable (attribute), for all parent class variables (attributes), you can call in the default state; for variables (attributes) that are not covered by the subclass, it can also be forced to convert after the subclass transfer;

5. If you want to call the subclass-specific variable (attribute), you can only call with the subcatenus. 6. If you want to call the subclass-specific method, you can only use it in the case of being forced by the subclass.

The following example mixed the above, experience it.

Public class temp {public static void main (String [] args) {super sub = new sub (); // call the parent class unique method system.out.println ("Father class unique method"); Sub.print ); (Sa) .print (); (sub) sub) .print (); // call the parent class unique variable (attribute) System.out.println ("Parent class unique variable (attribute) "); System.out.println (sub.supown); system.out.println ((sub) sub); // Call the method of the covered parent class system.out.println (" covered Method of parent class "); super sup = new super (); sup.plus (3); // Call the variable (attribute) of the covered parent class (") system.out.println ("The parent class overwritten Variable (attribute) "); system.out.println (sub.i); system.out.println (sub.both); // Call subclass-specific method system.out.println (" Sub-class unique method " ); (Sub) sub) .reduce (3); // Call the subclass-specific variable (attribute) System.out.Println ("subclass-specific variable (attribute)"); system.out.println ((( (SUB) SUB) .SUBOWN; // Method for the subclass of the parent class system.out.println ("Method for the subclass of the parent class"); Sub.plus (3); ((SUB) ) SUB) .plus (3); ((super) sub) .plus (3); // Method for calling the covered subclass system.out.println ("overwrites the variable of the subclass of the parent class (attribute) "); System.out.println ((sub) sub) .i); System.out.Println ((sub) Sub) .both; }}} class super {protected int i = 0; string Both = "super's Both", supown = "super's oowload"; Void Plus (INT i) {system.out.println ("super's plus);} void print ) {System.out.println ("super's own print");}}} Class Sub Extends Super {Protected Int i = 1; String Both = "Sub's Both", Subown = "Sub's Own Subown"; Void Plus (INT i) {System.out.Println ("Sub's Plus");} Void Plus (INT I, INT J) {System.out.Println ("Sub's Own Plus");

} Void Reduce (INT I) {System.out.println ("Sub's Own Reduce");}} The result is as follows:

Parent class unique

Super's OWN Print

Super's OWN Print

Super's OWN Print

Parent class specific variable (attribute)

Super's OWN Supown

Super's OWN Supown

Super's OWN Supown

Method for covered parent class

Super's Plus

The variable (attribute) of the parent class being covered

0

Super's Both

Sub-class unique method

Sub's OWN Reduce

Sub-class specific variable (attribute)

Sub's OWN Subown

Method for covering subclasses of parent classes

Sub's Plus

Sub's Plus

Sub's Plus

Covered the variable of the subclass of the parent class (attribute)

1

Sub's Both

Summary for the third point is as follows:

Through the above example, we can draw the following summary:

For objects created in this way, in addition to quilt classes

In addition to the covered parent class, it can access the subclass and the parent class.

Equipment attributes and methods. The rest of the subclass is called

Perform after compulsory conversion (removing the method of inheriting from the parent class)

Please see friends commented!

转载请注明原文地址:https://www.9cbs.com/read-6176.html

New Post(0)