1, Use and test cases of polymorphism
package com.yjxxt.oop01; public class Class07_Practice { /* * Four principles for doing questions: * 1, Inheritance chain: you didn't find a parent * A * | * B * / \ * C D * 2, Compile to see the type, determine the method, and run to find the object * * 3, Nearest optimal principle * * 4, The parent class reference is not visible to the new method of the child class */ public static void main(String[] args) { A a1=new A();//Parent method table: "A and D","A and A" A a2=new B();//Polymorphic method table: "A and D","B and A" B b =new B();//Method table: "B and A","B and B","A and D","A and A" C c=new C();//Method table: "B and A","B and B","A and D","A and A" D d =new D();//Method table: "B and A","B and B","A and D","A and A" System.out.println(a1.show(b)); //"A and A" System.out.println(a1.show(c)); //"A and A" System.out.println(a1.show(d)); //"A and D" System.out.println(a2.show(b)); //"B and A" System.out.println(a2.show(c)); //"B and A" System.out.println(a2.show(d)); //"A and D" System.out.println(b.show(b)); //"B and B" System.out.println(b.show(c)); //"B and B" System.out.println(b.show(d)); //"A and D" } } class A{ public String show(D obj){ return ("A and D"); } public String show(A obj){ return ("A and A"); } } //Subclass class B extends A{ //New method public String show(B obj){ return ("B and B"); } //override method public String show(A obj){ return ("B and A"); } } class C extends B{ } class D extends B{ }
2, Polymorphism: Transformation
Someone asked Confucius' father to give a lecture, but unfortunately Confucius' father was out and Confucius was at home
Upward Transformation: Confucius decided to make up and give lectures as Confucius' father
KongZiDie zi = new KongZi(); polymorphic
zi.teach(); Overridden methods in subclasses
Confucius and students play games together. If they want to call the new content in the subclass, they need to transform downward
Downward Transformation: the parent class reference is converted to the corresponding subclass type
KongZi z = (KongZi)zi; //KongZi z = new KongZi();
z.play();
ClassCastException: type conversion exception, which is converted to non other subclass types during downward transformation - > brother z = new kongzi();
instanceof operator: avoid the occurrence of type conversion exceptions during transformation, and judge before transformation
Reference the instanceof type to determine whether the previous reference points to the object of the following type | subclass object. If it returns true, not false
public class Class08_Cast { public static void main(String[] args) { //Polymorphism, upward transformation KongZiDie k=new KongZi(); k.teach(); //Turn down if(k instanceof Brother){ Brother b=(Brother)k; b.play(); b.teach(); } } } class KongZiDie{ void teach(){ System.out.println("do business"); } } class KongZi extends KongZiDie{ void teach(){ System.out.println("The Analects of Confucius"); } void play(){ System.out.println("Tease the cat"); } } class Brother extends KongZiDie{ void teach(){ System.out.println("Analects 1"); } void play(){ System.out.println("Tease cat 1"); } }
3, Polymorphism: abstract usage and analysis
Abstract class: Abstract abstract abstract
Abstract modified class -- > abstract class
Method modified by abstract --> Abstract method
No method body
Abstract methods must be defined in an abstract class
Develop ment Department work
Java -- > work: server side development
db --> work: data processing
be careful:
1. Abstract classes cannot be instantiated
2. Abstract classes can define abstract methods, non abstract methods, properties, constructors
3. Abstract methods must be defined in the abstract class
4. Use of abstract classes: call through specific subclass objects
Specific subclasses: override all abstract methods + add as needed
Abstract subclass: override abstract methods on demand + add on demand
5. Once an abstract method is overridden, it can no longer be overridden in subsequent subclasses and can be overridden as needed
6. Abstract methods must be rewritten. They cannot be called without rewriting and method body
7.abstract cannot be used with static,private,final and native
public class Class09_Abstract { public static void main(String[] args) { } } abstract class Develop{ //attribute int i; static int j=2; //Abstract method body: if the method body does not know what to define or how to define, it will not be defined abstract void work(); abstract void work1(); //Common method public void test(){ System.out.println("Common methods in abstract classes"); } } //Specific subclass class Java extends Develop{ @Override void work() { System.out.println("Knock the code and sleep"); } @Override void work1() { System.out.println("Rewritten method.."); } //New method void sleep(){ System.out.println("Sleep with your eyes open..."); } } //Abstract subclass abstract class DB extends Develop{ @Override void work() { System.out.println("Rewritten work"); } } class Demo1 extends DB{ @Override void work1() { System.out.println("I am Demo Middle work2"); } }
4, Polymorphism: Interface usage and analysis
/*
Interface:*****
Special abstract classes
Interface is a reference data type
Interfaces are collections of abstract functions
Class single inheritance mechanism, and interface is multi implementation mechanism
Interface definition development specification
Decoupling and reducing coupling
definition:
Interface defines the interface
Interface - > implement interface through implements
Parent class - > you need to inherit the parent class through extensions
Difference between inheritance and Implementation:
If a subclass inherits from the parent class, it can directly have the right to use the members in the parent class (focus: use it directly)
The implementation class implements the interface. The implementation class has the functions defined by the interface, but the functions are abstract and have no method body. It needs to be implemented by the implementation class itself (focusing on the implementation of methods, which is called implementation class)
jdk1.7 and before
1. Public static constants Public static final - > any omission
2. Public abstract method Public abstract - > omit arbitrarily
jdk1.8 and later
use:
1. The interface cannot be instantiated
2. The interface needs to be implemented through the implementation class
Concrete implementation classes: override all abstract methods + Add on demand
Abstract implementation classes: Rewrite abstract methods on demand + add new methods on demand
3. Classes can only be inherited by classes, and interfaces can only be implemented by classes
4. A class can only inherit a single parent class, but can implement multiple interfaces
5. Inherit the parent class before implementing the interface
6. One interface can inherit multiple interfaces, and interfaces can inherit multiple interfaces
7. Avoid the same method in multiple interfaces implemented
public class Class11_Interface { public static void main(String[] args) { System.out.println(Myinterface.PI); //Implementation class object Impl1 impl1=new Impl1(); impl1.haha(); impl1.hehe(); } } //Interface interface Myinterface{ //1. Public static constants double PI=3.14; //2. Public abstract method void haha(); void hehe(); } //Concrete implementation class class Impl1 implements Myinterface{ @Override public void haha() { System.out.println("Chat ends with a smile...."); } @Override public void hehe() { System.out.println("Chat ends in hehe...."); } } //Abstract implementation class abstract class Impl2 implements Myinterface{ @Override public void haha() { } } //Specific subclasses class Son extends Impl2{ @Override public void hehe() { } }
4.1, Interface test case II
package com.yjxxt.oop01; /* 4.Class can only inherit a single parent class, but can implement multiple interfaces 5.Inherit the parent class before implementing the interface */ public class Class12_Interface1 extends Object implements D1{ @Override public void a() { } @Override public void b() { } @Override public void c() { } } interface A1{ void a(); } interface B1{ void b(); } interface C1{ void c(); } interface D1 extends A1,B1,C1{ }
four point two Interface test case III
/* Connect the computer to an external USB device USB Interface specification: start using equipment and end using equipment computer External USB devices (mouse, keyboard, hard disk...): realize USB interface specification Note: the parent class and interface cannot completely replace each other. It is recommended to use the interface Summary: The difference between abstract classes and interfaces .... */ public class Class12_PracticeUsb { public static void main(String[] args) { Computer computer=new Computer(); Mouse mouse=new Mouse(); computer.close(); computer.useUSB(mouse); computer.close(); } } //USB interface interface USB{ void start(); void end(); } //computer class Computer{ private String type; //Empty structure public Computer() { } //Accessor setter public String getType() { return type; } public void setType(String type) { this.type = type; } public void open(){ System.out.println("Power on...."); } public void close(){ System.out.println("Shut down...."); } //Using an external USB device public void useUSB(USB usb){// //USB usb = new KeyBoards(); Interface polymorphism usb.start(); usb.end(); } } //External equipment: brand color price //mouse class Mouse implements USB{ @Override public void end() { System.out.println("Start using the mouse..."); } @Override public void start() { System.out.println("End using mouse..."); } } //keyboard class Keyboards implements USB{ @Override public void start() { System.out.println("Start using the keyboard..."); } @Override public void end() { System.out.println("End using keyboard..."); } }