Basic Learning day08 --- polymorphism, simple factory, Object class equals, toString, day08 --- equals
I. Polymorphism
1.1. Concept of Polymorphism
Definition: multiple forms of existence of a certain type of things.
For example, cats and dogs in animals.
The cat object corresponds to the cat type: Cat x = new CAT ();
At the same time, a cat is also a type of animal. It can also be called an animal: Animal y = new CAT ();
Animals are the parent type extracted from the specific thing of cats and dogs.
The parent type reference points to the subclass object. Embodiment:
The reference of the parent class or interface points to or receives its own subclass object.
Purpose:
The existence of polymorphism improves program scalability and maintainability in the future.
Prerequisites:
1. There must be inheritance or implementation relationships.
2. Overwrite operations are required.
Benefits:
This improves code scalability, and the code defined in the earlier stage can use later content.
1.2 and 2 types
Upward and downward Transformations
1.3 upward Transformation
Declared as the parent type, such as animal bird = new bird ();
But the class line for the subclass at runtime
Any subclass can be replaced by a parent class.
Package com. day08.doutai. demo1; public abstract class Animal {private String name; private String gender;/*** meal Method */public abstract void eat (); public String getName () {return name ;} public void setName (String name) {this. name = name;} public String getGender () {return gender;} public void setGender (String gender) {this. gender = gender ;}}
Package com. day08.doutai. demo1;/*** birds eat bugs * @ author denny **/public class Bird extends Animal {@ Override public void eat () {System. out. println ("Birds eat insects");} public void fly () {System. out. println ("Birds fly in the sky ");}}
Package com. day08.doutai. demo1;/*** Dog bone eating * @ author denny **/public class Dog extends Animal {@ Override public void eat () {System. out. println ("Puppies eat bones");} public void run () {System. out. println ("the puppy runs on the ground! ");}}
Package com. day08.doutai. demo1;/*** Piglet class * @ author denny **/public class Pig extends Animal {@ Override public void eat () {System. out. println ("piglets eat miscellaneous food! ");} Public void sleep () {System. out. println (" pig sleeping! ");}}
Test class
Package com. day08.doutai. demo1; /*** multi-state upward transformation * declares that the State is parent class * The runtime state is sub-class * @ author denny */public class AnimalTest1 {public static void main (String [] args) {// declare Animal dog = new Dog (); // declare Animal pig = new Pig (); // declare Animal bird = new Bird (); getAnimal (dog); getAnimal (pig); getAnimal (bird);} public static void getAnimal (Animal a) {. eat ();}}
Result:
Pig, pig, and pig! Birds eat insects
1.4 downward Transformation
Determines whether it is a subclass. If yes, it forces type conversion.
Intanceof keyword to judge
The above classes remain unchanged
Package com. day08.doutai. demo1; /*** multi-state upward transformation * declares that the State is parent class * The runtime state is sub-class * @ author denny */public class AnimalTest1 {public static void main (String [] args) {// declare Animal dog = new Dog (); // declare Animal pig = new Pig (); // declare Animal bird = new Bird (); getAnimal (dog); getAnimal (pig); getAnimal (bird);} public static void getAnimal (Animal animal) {// judge whether it is a puppy class if (animal instanceof Dog) {Dog dog = (Dog) animal; dog. run (); // judge whether it is a Bird} else if (animal instanceof Bird) {bird Bird = (bird) animal; Bird. fly ();} else if (animal instanceof Pig) {Pig pig = (Pig) animal; pig. sleep ();}}}
Result:
The puppy is running on the ground! Pig is sleeping! Birds fly in the sky
1.5. Simple Factory
Package com. day08.doutai. demo1; public class AnimalFactory {Animal an; public Animal getAmincal (String animal) {if (animal. equalsIgnoreCase ("bird") {an = new Bird ();} else if (animal. equalsIgnoreCase ("pig") {an = new Pig ();} else if (animal. repeated signorecase ("dog") {an = new Dog ();} else {System. out. println ("no such animal");} return ;}}
Test class
Package com. day08.doutai. demo1; public class Test1 {public static void main (String [] args) {// declare a factory class AnimalFactory factory = new AnimalFactory (); Animal animal = factory. getAmincal ("dog"); animal. eat ();}}
Result:
Puppies eat bones
1.6 Characteristics of member variables in Polymorphism
Features of member functions:
During the compilation period: see whether this call method exists in the class to which the referenced type variable belongs. If so, the compilation passes and if no compilation fails.
In runtime: see whether the method is called in the class to which the object belongs.
Simple: when a member function is called with polymorphism, compile the code to the left and run the function to the right.
Regardless of compilation and running, refer to the left side (class to which the referenced type variable belongs ).
For static members, refer to the left
Package com. day08.demo1;/***** parent class * @ author Denny **/public class Fu {// member variable int num = 5;} package com. day08.demo1; public class Zi extends Fu {int num = 8;} package com. day08.demo1; public class Test1 {public static void main (String [] args) {Fu f = new Zi (); System. out. println (f. num); // 5 Zi z = new Zi (); System. out. println (z. num); // 8 }}
Result:
5
8
Ii. Object Class
2.1. Object Class
The Object class is the root class (indirect parent class) of all classes, and the top class of the Java Object.
This class defines the functions of all objects
2.2. Sort Se () method
Package com. day08.objec. demo1;/*** human * @ author Administrator **/public class Person {/*** name * age * Gender */private String name; private int age; private String gender;/*** getter and setter Methods * @ return */public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public String getGender () {return gender;} public void setGender (String gender) {this. gender = gender ;}}
Test class
package com.day08.objec.demo1;public class Test1 { public static void main(String[] args) { Person p1 = new Person(); Person p2 = new Person(); Person p3 = p1; System.out.println(p1 == p2); System.out.println(p1.equals(p2)); System.out.println(p1 == p3); System.out.println(p1.equals(p3)); }}
Result:
False
False
True
True
Override the equals Method
/*** Override the sort se Method */@ Override public boolean equals (Object obj) {// determine whether the Object is this if (this = obj) {return true ;} // if it is null, false is returned. if (null = obj) {return false;} if (obj instanceof Person) {Person p = (Person) obj; return name. equals (p. getName () & age = p. getAge () & gender. equals (p. getGender ();} else {return false ;}}
Test
Package com. day08.objec. demo1; public class Test1 {public static void main (String [] args) {Person p1 = new Person (); p1.setName ("Zhang San"); p1.setAge (20 ); p1.setGender ("male"); Person p2 = new Person (); p2.setName (""); p2.setAge (21); p2.setGender ("female"); Person p3 = p1; p3.setName ("Zhang San"); p3.setAge (20); p3.setGender ("male"); System. out. println (p1.equals (p2); System. out. println (p1.equals (p3); System. out. println (p1.hashCode (); System. out. println (p2.hashCode (); System. out. println (p3.hashCode ());}}
Result:
False
True
705927765
366712642
705927765
2.3. toString () Class
ToString = getClass (). getName + @ + Integer. toHexString (hasCode ())
Or the Person class above
Package com. day08.objec. demo1; public class Test2 {public static void main (String [] args) {Person p1 = new Person (); System. out. println (p1.toString (); // The default value is hexadecimal System. out. println (p1.hashCode (); // The default value is 10 hexadecimal System. out. println (Integer. toHexString (p1.hashCode (); // convert to hexadecimal System. out. println (p1.getClass (). getName () + "@" + Integer. toHexString (p1.hashCode ()));}}
Result:
Com. day08.objec. demo1.Person @ 2a139a55
705927765
2a139a55
Com. day08.objec. demo1.Person @ 2a139a55