Table of contents
1. Security perspective (encapsulation)
2. Angle of flexibility (pull and move the whole body)
1. How to implement polymorphism
1. Complete upward transformation
Direct assignment:
3. Point to the subclass object through the parent class reference (overridden method)
Parent class references point to subclass objects:
3. The application of polymorphic thinking (drawing graphics)
Four. The benefits of polymorphism:
1.To summarize the difference between rewriting and overloading:
---
Preface
These contents are a review of the basic part of javase
---
Inheritance and Composition:
1. Relationally:
inheritance is a
combination has a
2. In terms of usage:
Both composition and inheritance can achieve code reuse. Whether to use inheritance or composition depends on the application scenario. The general suggestion is to use composition as much as possible.
>Similar to building a car, a car can be composed of wheels, lights... and write the code as follows
Combined code:
```java //demo portfolio class Tire{ private int size; public void run(){ System.out.println("tire turning"); } } class Light{ public void bright(){ System.out.println("Light"); } } public class Vehicle { private Tire tire; private Light light; public Vehicle(Tire tire,Light light){ this.tire=tire; this.light=light; } public void operation(){ tire.run(); light.bright(); } public static void main(String[] args) { Tire tire=new Tire(); Light light=new Light(); Vehicle vehicle=new Vehicle(tire,light); vehicle.operation(); } }
difference and connection
1. Security perspective (encapsulation)
The combination is assembled together. The internal implementation details are invisible and have better encapsulation (black box)
In an inheritance structure, the internal details of the parent class are visible to the child class. So we can usually say that code reuse through inheritance is a kind of white-box code reuse. (If the implementation of the base class changes, the implementation of the derived class will also change. This leads to unpredictable behavior of the subclass ;)
For example: the method of the class is used by other classes, in case other classes rewrite the method, this method will have an impact on the follow-up (all used)
2. Angle of flexibility (pull and move the whole body)
Inheritance, when writing code, it is necessary to specify which class to inherit, so the relationship is determined at compile time. (The implementation inherited from the base class cannot be changed dynamically at runtime, thus reducing the flexibility of the application.)
For example: callback (every time you inherit, call back the methods and properties in it)
Combination, interface-oriented programming can be used when writing code. Therefore, the composition relationship of classes is generally determined at runtime
Polymorphism
1. How to implement polymorphism
1. Complete upward transformation
Pay attention to three points
Direct assignment:
Directly assign subclass object to parent class
Animal animal=new Bird("Parrot",2); animal.eat()
At this time, the parent class reference can only access its own methods. If you want to access the unique methods of the subclass, you need to downcast
method parameter
method return value
2. Complete method rewriting:
Pay attention to 6 points
animal.eat();
The eat() of the subclass is called
The above process is actually a method rewriting
The conditions for method rewriting are met:
1. The method name is the same,
2. The parameter list is the same [number, type, order]
3. The return value is the same (or the return value of the subclass and the parent class form a parent-child relationship (covariant type))
This is the subclass
Here's the parent class:
public Animal eat() { System.out.println(name + "can cook"); return new Animal("animal",18); }
Since the return value of the subclass and the parent class form a parent-child relationship, the running result is still the eat of the subclass
4. The static modification method cannot be overridden
5. The private modification method cannot be overridden (private can only be used in the current class)
6. The access modifier of the subclass needs to be greater than or equal to the access modifier of the parent class
Overriding the parent class method
3. Point to the subclass object through the parent class reference (overridden method)
dynamic binding
dynamic binding
Also called runtime binding, both subclasses and parent classes have this method
When compiling, it still calls the parent class itself, but at runtime, the program undergoes dynamic binding --- the subclass's own
This is what it looks like when compiled:
At runtime, the compiler quietly changes the address of the animal.eat method to 0x56
Parent class references point to subclass objects:
// Bird bird=new Bird("Parrot",2); // Animal animal=bird;
Animal animal=new Bird("Parrot",2);
animal.eat()
At this time, the parent class reference can only access its own methods. If you want to access the unique methods of the subclass, you need to downcast
2. What is polymorphism:
public static void function(Animal animal) { animal.eat();//The caller doesn't care who eats } public static void main(String[] args) { function(new Bird("Parrot",2)); function(new Dog("dog",1)); }
The parent class reference refers to the subclass object. Through the parent class reference, the rewritten method is called. At this time, dynamic binding occurs. At this time, the parent class reference is only this one, but the object I reference is different. By calling the same method, we found that the form of expression at this time is different, and this phenomenon is called polymorphism
3. The application of polymorphic thinking (drawing graphics)
package polymorphism; class Shape{ public void draw(){ System.out.println("draw graphics"); } } class Rect extends Shape{ @Override public void draw() { System.out.println("rectangle"); } } class Cycle extends Shape{ @Override public void draw() { System.out.println("round"); } } class Triangle extends Shape{ @Override public void draw() { System.out.println("△"); } } public class Test2 { public static void drawMAP(Shape shape){ shape.draw(); } public static void main(String[] args) { Rect rect=new Rect(); Cycle cycle=new Cycle(); Triangle triangle=new Triangle(); drawMAP(rect); drawMAP(cycle); drawMAP(triangle); } }
Suppose the graphics to be drawn now are in the shape array
public static void main(String[] args) { Rect rect=new Rect(); Cycle cycle=new Cycle(); Triangle triangle=new Triangle(); Shape[]shapes={rect,cycle,rect,cycle,rect,triangle}; for (Shape shape:shapes) { //If the current shape reference object rewrites the draw method, it will be called, if not, it will call its own (already up-transformed and dynamically bound) shape.draw(); } }
If not using polymorphism
Four. The benefits of polymorphism:
1. Reduce the cyclomatic complexity (reduce the number of conditional loop statements)
2. Strong expansion ability
to add on:
To summarize the difference between rewriting and overloading:
Method overloading is static binding. At compile time, the specific method is known according to the actual parameter type at compile time.
Method rewriting is dynamic binding. At runtime, determine which method to call,
Rewriting is to rewrite and build another bow and arrow. Overloading is to add functions on the original basis.
That is: method overloading is a polymorphic expression of a class, and method rewriting is a polymorphic expression of a subclass and a parent class
Downward transformation
unsafe:
Compile without error:
//Indicates downcasting is not safe public static void main(String[] args) { Animal animal = new Dog("dog", 16); Bird bird = (Bird) animal; bird.fly(); } public static void main1(String[] args) { Animal animal=new Bird("the bird",2); Bird bird=(Bird)animal;//Downward transformation, parent class type to subclass type bird.fly(); }
When running, main displays: (a dog is not a bird)
main1 is fine because subclasses have this property (birds are animals)
Modify the code:
Introduce instance of
public static void main(String[] args) { Animal animal = new Dog("dog", 16); if (animal instanceof Bird) { //Not all animals are birds Bird bird = (Bird) animal; bird.fly(); } }
Gouzi is not a bird. Obviously, there is no output and cannot be transformed downwards