Chapter 8. Object-oriented (polymorphic-virtual method)

Source: Internet
Author: User

namespacepolymorphic { Public classPerson {Private string_name;  Public stringName {Get{return_name;} Set{_name =value;} }         PublicPerson (stringname) {             This. Name =name; }        //virtual Method         Public Virtual voidSayHello () {Console.WriteLine ("I am a human being"); }    }     Public classChinese:person { PublicChinese (stringname):Base(name) {}//method Overrides         Public Override voidSayHello () {Console.WriteLine ("I am Chinese, my name is {0}", This.        Name); }    }     Public classJapanese:person { PublicJapanese (stringname):Base(name) {}//method Overrides         Public Override voidSayHello () {Console.WriteLine ("I'm Japanese, my name is {0}", This.        Name); }    }     Public classKorean:person { PublicKorean (stringname):Base(name) {}//method Overrides         Public Override voidSayHello () {Console.WriteLine ("I'm Korean, my name is {0} .", This.        Name); }    }     Public classAmerican:person { PublicAmerican (stringname):Base(name) {}//method Overrides         Public Override voidSayHello () {Console.WriteLine ("I'm an American, my name is {0}", This.        Name); }    }     Public classEnglishman:person { PublicEnglishman (stringname):Base(name) {}//method Overrides         Public Override voidSayHello () {Console.WriteLine ("I'm English, my name is {0}", This.        Name); }    }}
classProgram {Static voidMain (string[] args) {Chinese cn1=NewChinese ("Chengjiayang"); Chinese cn2=NewChinese ("Wang Xudong"); Japanese J1=NewJapanese ("under the tree, June"); Japanese J2=NewJapanese ("Well Edge Sub"); Korean K1=NewKorean ("Ronaphoom"); Korean K2=NewKorean ("Seok"); American A1=NewAmerican ("Adele"); American A2=NewAmerican ("Little Britney Spears ."); Person[] P={cn1, cn2, J1, J2, K1, K2, A1 , A2,NewEnglishman ("Green"),NewEnglishman ("Maria") };  for(inti =0; i < p.length; i++) {P[i].            SayHello ();    } console.readline (); }


Chapter 8. Object-oriented (polymorphic-virtual method)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.