[C #] What about OOP polymorphism,
Advice to cainiao developers: spend 10 thousand hours practicing Coding and do not waste 10 thousand hours Debugging (that is, reading code)
Looking at the above UML diagram, we create an abstract Instrument class with an abstract method paly in the class, and all the subclasses inherit this class and implement the paly method. (If you do not understand inheritance, refer to another article: inheritance of OOP)
Let's take a look at the implementation of the class:
Public abstract class Instrument {public abstract void Play ();} public class Guitor: Instrument {public override void Play () {Console. writeLine (string. format ("Play {0}", this. getType (). name) ;}} public class Paino: Instrument {public override void Play () {Console. writeLine (string. format ("Play {0}", this. getType (). name) ;}} public class Violin: Instrument {public override void Play () {Console. writeLine (string. format ("Play {0}", this. getType (). name ));}}View Code
In each subclass method, we directly exit the string.
Class Program {static void Main (string [] args) {Guitor g = new Guitor (); g. play (); Paino p = new Paino (); p. play (); Violin v = new Violin (); v. play (); Console. readLine ();}}View Code
In the Main method, three objects are created and output respectively. This is the way to write code before we start polymorphism? Paly, which writes 100 instruments in the corner). What is polymorphism?
What is polymorphism: multiple forms of manifestation (good abstraction ...)
THINK written in java: polymorphism is to eliminate the coupling between types (or abstract)
Let's look at the example:
Class Program {static void Main (string [] args) {PlayInstrument (new Guitor (); PlayInstrument (new Paino (); PlayInstrument (new Violin (); Console. readLine ();} public static void PlayInstrument (Instrument instrument) {instrument. play ();}}View Code
This output is the same as the above example. What have we changed? We provide a method to include the references of the three subclasses, why is PlayInstrument's form parameter an Instrument object? This is the key to polymorphism,
I have summarized one sentence: the reference of the subclass (new Guitor () points to the parent class Object (Instrument instrument );
Static void Main (string [] args) {Instrument instruments = new Guitor (); instrument = new Paino (); instrument = new Violin (); Console. ReadLine ();}View Code
This is amazing, and I think it's amazing when I was a beginner. Let me lead you into the mysterious multi-state space.
Upward Transformation
Static void Main (string [] args) {Guitor gutior = new Guitor (); Instrument instrument = gutior; Console. ReadLine ();}View Code
In the upward transformation, it is safe to convert the subclass type to the parent class. Looking at the example above, if you view it in the vs ide tool, no compilation error is reported, why? Let's look at the figure.
We can see this circle as a heap (this is not a stack, because it points to the security of the upward transformation ). let's take a look at Guitor, which contains all the things of instruments (except the construction, if you cannot find something private, you can try reflection, it does exist ), in this way, is it safe to turn a bulk into a small block, because I have everything in the small block, so it is safe to transform upwards.
Note: on the contrary, downward transformation is dangerous because small pieces become large blocks and you cannot control the contents. (The concept of covariant is similar to that of inverter. I prefer to call it a harmonious change-> Security, reverse-day change-> danger)
In this case, we can convert large blocks into small blocks (Instrument instrument = new Guitor (), and we can only call small pieces of things (so when we design, the interface is usually used to limit)
In fact, the interface should be pulled out separately. However, because there are not many basic concepts of the interface, the interface is very useful in our OO design. Here it is within the scope of the design. In polymorphism, use the interface as the parent class is the same as using the class method. You can try it.
Summary:
1. After the upward transformation, although the reference is still the original subclass, you can only use the parent class method (think about small blocks)
2. after the parent method is overwritten by the quilt class (virtual, override), after the upward transformation, calling the same method is still large (because the small pieces are overwritten, it feels very far-fetched, if we can still use the base keyword in the subclass to call the method with the same name as the parent class, as for the memory call in this section, I have not found any relevant information. If anyone knows this, please share it, thank you very much ).
3. interfaces and abstract classes (classes) use the same form of polymorphism.
I feel this is not well written... hope you can add and point out the shortcomings. Thank you.
What is CC?
Many abbreviations
It can also be the name of the heroine of the rebellious luch.
[C Language] Is there a function that can clear keys in the cache?
Fflush (stdin)
Clear standard input Cache
# Include "stdio. h"
Main ()
{
Char a, B;
Scanf ("% c", & );
// Fflush (stdin );
Scanf ("% c", & B );
Printf ("\ n % c", a, B );
}
You can try it. If there is no fflush (stdin), if you enter a string of characters "abcd", a = 'A', B = 'B'
If fflush (stdin) exists, after entering "abcd", the program continues to wait for the input, and then enters "efdfsd". The result is a = 'A', B = 'E'