Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What exactly is the concept of polymorphism in C #

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Many novices are not very clear about the concept of polymorphism in C#. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

I believe everyone is familiar with the three features of object-oriented encapsulation, inheritance, and polymorphism, and everyone can say one or two words, but most of them only know what these are and do not know how they are implemented within CLR, so this article mainly talks about the mechanism that some concepts in polymorphism have been implemented internally.

The concept of polymorphism

First of all, explain what polymorphism is: the same operation acts on different objects, there can be different interpretations, resulting in different implementation results, this is polymorphism. In other words, it is actually an instance of the same type that calls the "same" method and produces different results. The "same" here is put in double quotes because the same methods here only look the same, but in fact they call different methods.

When it comes to polymorphism, we can't unceremoniously mention the following concepts: overloading, overriding, virtual methods, abstract methods, and hidden methods. Let's introduce their concepts one by one.

1. Overloading (overload): two or more methods with the same function name and different parameter lists in the same scope (usually a class) are called overloading. They have three characteristics (commonly known as two must and one can):

The method name must be the same

The parameter list must be different

The return value type can be different.

For example:

PublicvoidSleep ()

{

Console.WriteLine ("Animal sleep")

}

PublicintSleep (inttime)

{

Console.WriteLine ("Animal {0} time to bed", time)

Returntime

}

2, rewrite (override): subclasses to meet their needs to repeatedly define different implementations of a method, need to use the override keyword, the method to be rewritten must be a virtual method, using the virtual keyword. It is characterized by (three identical):

Same method name

Same parameter list

Same return value

3. Virtual methods: methods defined in the base class that are allowed to be overridden in derived classes, defined using the virtual keyword. Such as:

PublicvirtualvoidEatFood ()

{

Console.WriteLine ("Animal eats")

}

Note: virtual methods can also be called directly.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report