inheritance
The keyword for explicit inheritance is extends
Inheritance in the upward and downward transformation
Upward transition: == A subclass object points to a reference to the superclass. Automatic transformation. == Downward transition: == Superclass objects point to subclass references. A forced transition is needed. = =
polymorphism
Polymorphism 㕛 represents the characteristic of multiple formal capabilities, with different instances of the same interface performing different operations. That is, different manifestations of the same kind of transaction.
Necessary conditions for polymorphism:
- inheritance
- Overrides methods of the superclass
- A parent class reference points to a child class object
Advantages: Reduced code volume, improved code scalability and maintainability.
Two forms of expression:
- The parent class is used as the parameter of the method.
- The parent class as the return value of the method returns a reference to the subclass.
== Use the intanceof keyword to determine whether the reference variable is of a certain type. = =
combination
A property of a class is another class. Composition is a way to improve code reusability.
public class Apple{
private int num;
private Color color;
}
public class Color{
private String color;
}
Copy the code
== If you want your class to be more extensible, use composition more often than inheritance. = =
The difference between inheritance and composition
Characteristics of the | combination | inheritance |
---|---|---|
Relationship between | has – a | is – a |
coupling | Loose coupling | Tightly coupled |
Whether there is polymorphism | No polymorphism, no upward transition | The basis of polymorphism can be transformed upward |
period | == Run is bound == | == Compile-time binding == |
The agent
A proxy can be understood as: if A wants to call A method of the class, instead of calling it directly, A will create A proxy of object B in its own class and then call the method of B.
In this way, agents are no different from the above combination.
When using A proxy, class A does certain logical operations before and after calling A class B method. It’s not just for class B methods.
Design pattern: Static proxy. Blog.csdn.net/lingyiwin/a…
Design pattern: Dynamic proxies. Blog.csdn.net/lingyiwin/a…
Interface oriented programming features
-
You don’t care about implementation details
-
I care about the capabilities of the implementation class
-
Focus on conventions, regardless of the actual implementation of the interface.