C # basic consolidation (1)-polymorphism + simple factory,
Polymorphism
To briefly describe polymorphism, I personally understand it as follows:Through inheritance, the parent class defines the method, and the rest implementation is carried out by the Child class.
01 code
// Parent class Person {public virtual void skill () // vitrual-virtual method, the method can be overwritten {Console. writeLine ("Walking");} class Xiaoming: Person {public override void skill () // rewrite the parent class method {Console. writeLine ("James can sing");} class XiaoHu: Person {public override void skill () {Console. writeLine ("Xiaohu swimming");} class Program {static void Main (string [] args) {Person p1 = new Xiaoming (); // The ryth conversion principle can assign subclass values to parent class Person p2 = new XiaoHu (); p1.skill (); p2.skill (); Console. readKey ();}}
Output:
In this example, the Person class defines the skill method. The specific implementation of the method is carried out by the subclass.
02 if not overwritten
That is, if the override of the subclass is changed to new, the methods of the parent class and the subclass are independent, and the following code is executed:
// Parent class Person {public virtual void skill () {Console. writeLine ("Walking");} class Xiaoming: Person {public new void skill () // rewrite the parent class method {Console. writeLine ("James singing") ;}} class Program {static void Main (string [] args) {Person p1 = new Xiaoming (); // Lishi conversion principle Xiaoming p2 = new Xiaoming (); p1.skill (); p2.skill (); Console. readKey ();}}
Output:
We can see that p. skill (); here we can see. For the previous object type, run the skill () method in the class.Here, P1 belongs to the Person class and P2 belongs to the Xiaoming class.
Polymorphism + simple factory
If I want to input Xiao Ming and Xiao Hu. At this time, the system automatically displays what James will and what Xiaohu will do.
First, add a factory class.
In life, factories are used to process products. Here, the corresponding results are returned after processing based on input parameters.
All code
// Parent class Person {public virtual void skill () {Console. writeLine ("Walking");} class Xiaoming: Person // inherits the Person {public override void skill () // override the parent class method {Console. writeLine ("James can sing");} class XiaoHu: Person {public override void skill () {Console. writeLine ("Xiaohu swimming") ;}// Factory class -- added class Factory {public static Person instance (string Name) {switch (Name) {case "1 ": return new Xiaoming (); case "2": return new XiaoHu (); default: return null ;}} class Program {static void Main (string [] args) {Console. writeLine ("Enter the serial number to view the skill, enter 1, 2 .... "); Console. writeLine ("1. james "); Console. writeLine ("2. tiger "); Console. writeLine ("input:"); string res = Console. readLine (); Console. writeLine ("Result:"); Person p = Factory. instance (res); if (p! = Null) {p. skill () ;}else {Console. WriteLine ("this person is not found") ;}console. ReadKey ();}}
Execution result:
At this point, polymorphism + simple factory has been implemented.
Extended 1: abstract Implementation of Polymorphism
1. The previous virtual (virtual) Implementation of polymorphism has been completed. In fact, abstract Implementation of polymorphism is also similar.
2. The difference between abstract and virtual is that abstract has no method body and methods can be overwritten.
Implementation: You can change the parent class of the complete code to the following. The function is the same.
// Parent class abstract class Person {public abstract void skill (); // method has no method body}
Extension 2: interface implementation Polymorphism
1. This is not an inheritance, but an interface implementation method.
2. There is no method body for interface methods and abstractions.
Implementation: Change the parent class of the complete code above to an interface
// Interface Person {void skill ();}
Then, remove the override of the original subclass because the interface cannot be overwritten. Virtual (abstract) methods can be overwritten.
OK. The implementation of the interface polymorphism is also complete.