Home > Back-end >  I ask what is the meaning of object downward transition?
I ask what is the meaning of object downward transition?

Time:10-07

Good bosses, want to consult the object where the significance of the transformation of down, why will the superclass forced into subclasses, and call the method that subclass, rather than directly new a subclass of the object to invoke the method in a subclass, trouble, help to explain, because I am not trained, you really don't understand,

CodePudding user response:

I'm a little humble opinions:
The core idea: object-oriented polymorphism, the same object, can have different performance, for the same object, in a different class to instantiate, so he implementation is different,
Take a chestnut:
Buses, as a parent, bus station so many men and women can be seen as subclasses of waiting for the bus, the bus must be as long as people can get on the bus, no rules must be male or female, male and female get in this process is the upward transition, all the men and women as human beings, all need women to get off to buy food, when you get on this process is human transition down on a woman, if according to you, is there a man in a man's car, a woman in a woman's car? As unfavorable to the development and maintenance, which is object-oriented programming can solve the problem, the subclass is abstracted into the parent class to manage, as long as people can sit bus, for example, when the function of the need to use a specific subclass, then down transition, women get off to buy food, for example,
Upcasting is type safe, the man must be a woman is man, concrete to the abstract
Downward transition will involve type insecurity, people are not necessarily man, abstract to concrete
You can look at the Java collection class source code,

CodePudding user response:

You are not very understanding, the interface
A class can return more types, there is no need to write too much method,
Receive downward transition when receiving,

CodePudding user response:

1/f, positive solutions, polymorphism, simplify the development, make your code is elegant
  • Related