C #-----Polymorphism

Source: Internet
Author: User

The concept of polymorphism: The same method is called by different objects implemented by inheritance, which shows different behaviors, called polymorphic

Override Implementing Polymorphic

 Public classAnimal { Public Virtual voidEat () {Console.WriteLine ("Animal Eat"); }    }     Public classDog:animal { Public Override voidEat () {Console.WriteLine ("Dog Eat"); }    }     Public classWolfdog:dog { Public Override voidEat () {Console.WriteLine ("Wolfdog Eat"); }    }    classTester {Static voidMain (string[] args) {animal[] Animals=Newanimal[3]; animals[0] =NewAnimal (); animals[1] =NewDog (); animals[2] =NewWolfdog ();  for(inti =0; I <3; i++) {Animals[i].            Eat (); }        }}

Abstract-override Implementing Polymorphic

 Public Abstract classAnimal { Public Abstract voidEat (); }     Public classCat:animal { Public Override voidEat () {Console.WriteLine ("Cat Eat"); }    }     Public classDog:animal { Public Override voidEat () {Console.WriteLine ("Dog Eat"); }    }     Public classWolfdog:dog { Public Override voidEat () {Console.WriteLine ("Wolfdog Eat"); }    }    classTester {Static voidMain (string[] args) {animal[] Animals=Newanimal[3]; animals[0] =NewCat (); animals[1] =NewDog (); animals[2] =NewWolfdog ();  for(inti =0; I < animals. Length; i++) {Animals[i].            Eat (); }        }    }

C #-----Polymorphism

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.