* Object-oriented feature three: Polymorphism
* 1. What does polymorphism mean? Polymorphism, which can be understood as a variety of forms of a thing's phenotype.
* 1) method overloads and overrides 2) the polymorphism of the sub-class object
*
* 2. Subclass Object Polymorphism Use premise: ① to have class inheritance ② to have subclasses override the parent class method
*
* 3. The program runs into a compilation state and a running state.
* For polymorphism, at compile time, "look left", this reference variable is interpreted as the type of the parent class
* Runtime, "Look right", focus on the entity of the real object: The object of the subclass. Then the method of execution is the subclass rewrite.
*
* 4. The polymorphism of the subclass object is not used for attributes.
1 Public classTestperson {2 Public Static voidMain (string[] args) {3Person p =NewPerson ();4 p.eat ();5 P.walk ();6 7Man m =NewMan ();8 m.eat ();9 M.Walk ();Ten System.out.println (); One A //the polymorphism of a subclass object: a reference to a parent class to a subclass object -person P1 =NewMan ();//Upward Transformation - //virtual method invocation: Refers to the object entity of the subclass through a reference to the parent class, and when the method is called, the child class is actually executing the method of overriding the parent class the p1.eat (); - P1.walk (); -SYSTEM.OUT.PRINTLN ("$" + p1.id);//1001 - + //p1.smoking = null; - //p1.entertainment (); + Aperson P2 =NewWoman (); at p2.eat (); - P2.walk (); - //p2.shopping (); -Woman w = (Woman) P2;//transition downward, using strong-to-go: () - w.shopping (); - in //java.lang.ClassCastException - //Woman w1 = (Woman) p1; to //w1.shopping (); + - //Woman w2 = (Woman) new Man (); the //instanceof: * //format: Object A instanceof Class A: Determines whether object A is an instance of Class A. Yes, returns true; otherwise returns false $ //If A is an instance of Class A, then a must also be an instance of the parent class of Class A. Panax Notoginseng if(P1instanceofWoman) { -System.out.println ("hello!"); theWoman W1 =(Woman) p1; + w1.shopping (); A } the + if(P1instanceofMans) { -Man M1 =(man) p1; $ m1.entertainment (); $ } - - if(P1instanceofPerson ) { theSYSTEM.OUT.PRINTLN ("Hello! "); - }Wuyi the } - Wu Public voidShow (person p) {//Person p = new Mans (); - About } $ - - - classPerson { A PrivateString name; + Private intAge ; the intID = 1001; - PublicPerson () { $ Super(); the } the PublicPerson (String name,intAge ) { the Super(); the This. Name =name; - This. Age =Age ; in } the PublicString GetName () { the returnname; About } the Public voidsetName (String name) { the This. Name =name; the } + Public intGetage () { - returnAge ; the }Bayi Public voidSetage (intAge ) { the This. Age =Age ; the } - - Public voidWalk () { theSystem.out.println ("People walking"); the } the Public voideat () { theSYSTEM.OUT.PRINTLN ("People eat"); - } the the } the 94 the the the classMansextendsperson{98 Booleansmoking; About intID = 1002; - PublicMan () {101 Super();102 }103 104 PublicMans (Booleansmoking) { the Super();106 This. Smoking =smoking;107 }108 109 Public Booleanissmoking () { the returnsmoking;111 } the 113 Public voidSetsmoking (Booleansmoking) { the This. Smoking =smoking; the } the 117 Public voidWalk () {118System.out.println ("Men walking in a straight walk");119 } - Public voideat () {121System.out.println ("Men should eat more meat!") ");122 }123 124 Public voidEntertainment () { theSystem.out.println ("Men's Treat");126 }127 } - 129 the classWomanextendsperson{131 Private Booleanisbeauty; the 133 Public Booleanisbeauty () {134 returnisbeauty;135 }136 137 Public voidSetbeauty (Booleanisbeauty) {138 This. Isbeauty =isbeauty;139 } $ 141 PublicWoman () {142 Super();143 }144 145 PublicWoman (Booleanisbeauty) {146 Super();147 This. Isbeauty =isbeauty;148 }149 Public voidWalk () { MaxSystem.out.println ("A woman walking with a slim ... ");151 } the 153 Public voideat () {154SYSTEM.OUT.PRINTLN ("Women should eat less, lose weight");155 }156 Public voidShopping () {157System.out.println ("Women love Shopping");158 }159 the}
Java Learning--object-oriented feature three: polymorphism