Multi-State learning notes for object-oriented programming mechanisms in C #

Source: Internet
Author: User


The polymorphism of C #:

My understanding is that the same operation, acting on different objects, has different results, that is, the same method will have different implementations when it acts on different objects as needed.


C # polymorphism includes: interface polymorphism, inheritance polymorphism.

Which inherit polymorphism and also include polymorphism implemented by virtual method and implemented by abstract method

For example: The basic animals have a way of eating, but different animals eat things will not be the same, such as wolves eat meat, sheep eat grass, so "eat" This method will be in the derived class to re-implement the following, run, by pointing to the base class pointer, to invoke the implementation of methods in the derived class.

Next, for example, polymorphism.


1. Interface polymorphism

Put the animal "eat" method into an interface (IAnimal), and then let the specific animal class (Wolf/sheep) inherit this interface, and according to their own needs to implement this interface.

Code implementation:


The code is as follows:


Class Program {

static void Main (string[] args) {

New Wolf (). Eat ();

New Sheep (). Eat ();

}

}

public class Wolf:ianimal {

Multi-State implementation

public void Eat () {

Console.WriteLine ("Wolf eats meat!") ”);

}

}

public class Sheep:ianimal {

Multi-State implementation

public void Eat () {

Console.WriteLine ("Sheep eat grass!") ”);

}

}

Interface

Public interface IAnimal {

void Eat ();

}

In addition to the Declaration,Running GuestArticles are original, reproduced please link to the form of the address of this article
Multi-State learning notes for object-oriented programming mechanisms in C #

This address: http://www.paobuke.com/develop/c-develop/pbk23185.html






Related content C # boxed and unboxing Operations example Analysis C # design Pattern Series Tutorial-Simple Factory mode C # call RAR file and get RAR return value Method C # implements two-time subtraction method
Examples of C # Speech recognition usages on the open closure principle of C # design mode C # Implementing the simplest text encryption Method C # Traverse a DataSet Control instance summary

Multi-State learning notes for object-oriented programming mechanisms in C #

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.