There are three ways to realize the polymorphism: virtual method, abstract class, interface, this article describes the first method: Virtual method.
1. What is a virtual method? Virtual methods, syntactically speaking, are modified by the virtual keyword, meaning that the method can be overridden in subclasses after the class is inherited.
2. What are the issues to be aware of overriding virtual methods in subclasses? The first thing to note is that overrides in subclasses are not necessary because the virtual methods in the parent class themselves also have a function body (unless it is an abstract method), so if overridden overrides a virtual method from the parent class, the method of the parent class is called directly.
3. How is polymorphism manifested? In the invocation of the subclass only need to call the same method, if the rewrite will show a different effect, which reflects the "polymorphic", that is, the same way of writing a different form of expression, "in the same to seek different."
4. code example:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespace_02 using virtual method to realize polymorphism7 {8 Public class Person9 {Ten PublicPerson (stringname) One { A This. Name =name; - } - Public stringName the { - Set; - Get; - } + //virtual methods, which can be overridden in subclasses - Public Virtual voidShow () + { AConsole.WriteLine ("I'm a person."); at } - } - /// <summary> - ///Chinese People - /// </summary> - Public classChinese:person in { - PublicChinese (stringname) to:Base(name) + { - the } * //rewrite the show () method $ //You can use the sealed keyword to decorate show () to see the No-rewrite show () method in the Beijingren classPanax Notoginseng Public Override voidShow () - { theConsole.WriteLine ("My name is: {0}, I am Chinese", Name); + } A Public voidSayHello () the { +Console.WriteLine ("Hello, I'm Chinese! "); - } $ } $ /// <summary> - ///Japanese people - /// </summary> the Public classJanpanese:person - {Wuyi PublicJanpanese (stringname) the:Base(name) - { Wu - } About Public Override voidShow () $ { -Console.WriteLine ("My name is: {0}, I am Japanese. ", Name); - } - } A /// <summary> + ///American Human the /// </summary> - Public classAmerican:person $ { the PublicAmerican (stringname) the:Base(name) the { the - } in //without rewriting, the method in the parent class person is called directly when the show () method is called with the American object. the //Public override void Show () the //{ About //Console.WriteLine ("I call: {0}, I am an American.") ", Name); the //} the } the Public classBeijingren:chinese + { - PublicBeijingren (stringname) the:Base(name)Bayi { the } the Public Override voidShow () - { -Console.WriteLine (""); the } the } the}
class definition
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespace_02 using virtual method to realize polymorphism7 {8 class Program9 {Ten Static voidMain (string[] args) One { AChinese CN1 =NewChinese ("Li Bai"); -Janpanese JP1 =NewJanpanese ("Panasonic's Help"); -American An1 =NewAmerican ("Obama"); the -person[] pn =NewPerson[] {cn1, JP1, an1};//create an array of the parent type - //iterate over the entire array and use is to determine whether it belongs to a type - for(inti =0; I < PN. Length; i++) + { - //This statement is a manifestation of polymorphism. + Pn[i]. Show (); A } at - //It turns out that polymorphism applies only to certain properties or methods common to subclasses, not to personal things. - //Person p1 = new Chinese ("SK"); - //P1. SayHello (); -Console.WriteLine ("OK"); - Console.readkey (); in - } to } +}
Main function
Multi-State realization--virtual method