/** * Richter Replacement principle: * Can use the parent class place, must be able to use the subclass * What is polymorphic: * Reference to the parent class, the object that points to the subclass * Polymorphism of the prerequisites: * There are two classes of inheritance relationship * polymorphism purposes: Re-*☆☆☆ code reuse * What to do when: * When assigning a value * *!!!!!!!!! ☆☆☆ Overwrite in polymorphic is a member method specified!!!!!!!!! * What is overwrite: * is the same method as the parent class in the subclass: Method Name, parameter list, return value type (and modifier list OK) * 1 Method name is the same: different, is two different method is not overwrite * 2 parameter list is the same: The parameter list is different, the method is overloaded, not overwrite * The number of parameter list and data type are the same. * 3 return The same value type; The return value represents function (method) function, overwrite is to function more powerful, cannot reduce function, so must be same * Overwrite after write cannot have lower access permission than original method (decrease of privilege means function is reduced) * cannot be more broad than the original method after overwrite Exceptions (which can only get worse and LOWER) * *!!!!! The ☆☆☆ property List of the parent class is generated at compile time: ☆☆☆* when it is JAVAC!!!!! The list of attributes for the ☆☆☆ subclass is generated at run time: that is, when Java is running ☆☆☆* * The invocation of the parent class and the subclass is in two forms: * 1 polymorphic parent class refers to the object of the child class the parent type variable name = new Subclass construction method (); * Static variables, static methods, member variables: * When you use the reference variable name to invoke the method or variable, first go to the parent class, if there is this variable (method). It will be executed directly, no Will go to the sub-class, if the parent does not have this variable (method), direct error (Error: Cannot find the symbol), will not go to the sub-class, because now the subclass of the attribute list is not, now is the compile time error, so can not go to sub-class to find * Member Method: * When calling the Member method, first go to the parent class to find, if the parent class has, then go to the subclass, if the subclass overwrite the member method, execute the subclass of the member method, if the subclass is not overwrite, also executes the parent class, but if the parent class does not have a member method , but the subclass has a direct error (Error: Cannot find the symbol), does not go to the subclass (no subclass-specific member methods are executed), because the subclass does not yet have a list of attributes, it is now only a compile-time error. * 2 Direct instantiation of subtype variable name = new Subclass construction method (); * Static variables, static methods, member variables: * First go to subclass to find, if the subclass has this variable (method), executes, no longer go to the parent class to find, if the subclass does not have this variable (method), it It goes to the parent class, executes the parent class, but if the subclass and the parent are not there, an error is added. * Member Method: * If the subclass has, the parent class also has, just as the subclass executes, that is the override of the method, if the subclass No, go to the parent class, execute the parent class, if the subclass has, the parent class does not, executes the subclass (for the execution of the subclass-specific method, is not the parent class), if not, then an error (Errors: Symbols not found) **/ Public classduo_tai{ Public Static voidMain (string[] args) {//The first polymorphic parent refers to an object that points to a child classFu_lei f =NewZi_lei (); //--------------------Variable------------------------------//the parent class has a static variable that has no child class inti =f.i; System. out. println (i);//output 100 is a static variable of the parent class//A static variable that has subclasses of the parent class intA = F.A;//The two variables here do not conflict because the preceding int a is a local variable, and the F.A is the static variable in the class.System. out. println (a);//Output 1 is a static variable of the parent class why the output is not a subclass of 111//the parent class has a member variable that has no child class intj =F.J; System. out. println (j);//output 200 is a member variable of the parent class//The parent class has child classes that have member variables intb =f.b; System. out. println (b);//Output 2 is a member variable of the parent class why the output is not a subclass of 2222 /*//Parent class does not have a static variable for the subclass why? Why not call the subclass? int m = F.M; DUO_TAI.JAVA:48: Error: Symbol SYSTEM.OUT.PRINTLN (m) not found; The parent class does not have a member variable for the child class why? Why not call the subclass? int n = F.N; DUO_TAI.JAVA:52: Error: Symbol SYSTEM.OUT.PRINTLN (n) not found; */ //--------------------Method------------------------------//static methods that do not have subclasses of the parent classF.M6 ();//static method of output parent class//A static method that has subclasses of the parent classF.M3 ();//static method of output parent class//the parent class has a member method that has no child classesF.M2 ();//output The member method of the parent class//The parent class has child classes that have member methodsF.M1 ();//output The member method of the parent class /*//Parent class There is no subclass of the static method why the error? Why not call the subclass? F.M5 (); DUO_TAI.JAVA:68: Error: Symbol not found//parent class has no child class the Member method why is the error? Why not call the subclass? F.M4 (); DUO_TAI.JAVA:71: Error: Symbol not found*///---------------------------------------------------------------------- //the second direct instantiation of a polymorphic subclass reference is a subclass of an objectZi_lei z =NewZi_lei (); //--------------------Variable------------------------------//the parent class has a static variable that has no child class intI1 = z.i;//output The static variables of the parent classSystem. out. println (I1);//Why is the subclass not error-free? Instead of calling the parent class's//A static variable that has subclasses of the parent class inta1 = Z.A;//static variables for output subclassesSystem. out. println (A1); //the parent class has a member variable that has no child class intJ1 = Z.J;//output the member variables of the parent classSystem. out. println (J1); //The parent class has child classes that have member variables intB1 = z.b;//member variables for output subclassesSystem. out. println (B1); //Parent class has no child class static variable intm1 = Z.M;//static variables for output subclassesSystem. out. println (M1);//Why does the parent class not have an error? //Parent class has no member variables for child classes intN1 = Z.N;//member variables for output subclassesSystem. out. println (N1); //--------------------Method------------------------------//static methods that do not have subclasses of the parent classZ.M6 ();//static method of output parent class Why didn't you get an error? and to invoke the static method of the parent class//A static method that has subclasses of the parent classZ.M3 ();//static methods for output subclasses//the parent class has a member method that has no child classesZ.M2 ();//the member method of the output parent class why didn't you get an error? and to invoke the Member method of the parent class//The parent class has child classes that have member methodsZ.M1 ();//the member method of the output subclass why?!!!! ☆☆☆//Parent class does not have a static method of subclassesZ.M5 ();//static methods for output subclasses//Parent class does not have a member method of child classZ.M4 ();//member methods for output subclasses }}classfu_lei{//declaring a parent class Static inti = -;//static variable The subclass of the parent class does not have a Static intA =1;//There are also static variable quantum classes . intj = $;//member variable The subclass of the parent class does not have a intb =2;//There are also subclasses of the parent class. Public voidM1 () {//overridden member variables (subclass some)System. out. println ("I am a member of the parent class method," ); } Public voidM2 () {//no overwrite member variable (subclass not)System. out. println ("I'm a member of the parent method, no overwrite" ); } Public Static voidM3 () {//static method subclasses also haveSystem. out. println ("I am a static variable of the parent class" ); } Public Static voidM6 () {//a static method that does not have a subclassSystem. out. println ("a static method that does not have a subclass" ); }}classZi_lei extends fu_lei{//declares a subclass inheritance (extends) parent class Static intm =3;//static variables not in parent class intn =8;//the parent class does not have a member variable Static intA =111;//there's a parent class . intb =2222;//there's a parent class . Public voidM1 () {//Parent class has member methodsSystem. out. println ("I'm a subclass, I overwrite the M1 () member method of the parent class" ); } Public voidM4 () {//the parent class does not have a member methodSystem. out. println ("I am a subclass, I this method, the parent class does not, is my own" ); } Public Static voidM3 () {//static methods for parent classesSystem. out. println ("I am the static method of the subclass, and the parent class also has the OH" ); } Public Static voidM5 () {//static methods that do not have a parent classSystem. out. println ("I am the static method of the subclass, the parent class does not," ); }}
Javase Review Diary: polymorphic