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

How to understand the Core Concepts in the OO World Polymorphism in .NET

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

Share

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

This article shows you the core concept of polymorphism in the OO world, .NET, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The word Polymorphism comes from biology, as the name implies, refers to a variety of forms. In the object-oriented world, polymorphism, inheritance and encapsulation constitute three core concepts.

Polymorphism in .NET usually means that the subclass is an evolution of the parent class. The subclass inherits from the parent class and has all the members (public or protected) defined by the parent class. But at the same time, it can modify (override or override) these members so that their implementation is completely different from that of the parent class and other subclasses. We can say that inheritance reflects the polymorphism of classes.

You should be familiar with the example of Duck, right?

Public abstract class Duck {public abstract void Quack ();} public class MallardDuck: Duck {public override void Quack () {Console.WriteLine ("Quack, quack, quack...");}} public class RubberDuck: Duck {public override void Quack () {Console.WriteLine ("Squeak, squeak, squeak...") }} public class Program {public static void Main () {Duck duck = new MallardDuck (); duck.Quack (); duck = new RubberDuck (); duck.Quack (); Console.ReadLine ();}}

Although both MallardDuck and RubberDuck inherit from the abstract class Duck and also have the Quack () method, they have different implementations and produce different results. When declaring a Duck type, it can be instantiated as Mallard, RubberDuck, or other classes inherited from Duck, and the implementation of each subclass is automatically invoked at run time.

These features of polymorphism make dependency injection and abstract-oriented programming possible, and its importance is self-evident.

Different polymorphisms

However, since polymorphism refers to different forms of the same kind of things, why should we limit our understanding of polymorphism to the inheritance relationship of classes? Is there still a polymorphism of non-inheritance relationships in .NET?

Generics reflect the polymorphism of parameters.

Type parameters are usually interpreted as placeholders in generics, but I prefer to understand them as an abstraction of parameters. With the most common List

< T>

For example, List

< string>

And List

< int>

The syntax is exactly the same, except that the type parameters are different, but they are two completely different classes. In other words, it is the difference in type parameters that leads to the shape of different classes.

Public class MyList

< T>

{private T [] items; private int size; public void Add (T item) {if (size = = items.Length) {/ / modify capacity} items [size++] = item;}}

If we use MyList

< string>

An array of strings is declared internally, and the argument to the Add method must also be string. If you use MyList

< int>

An int array is declared internally, and the parameter of the Add method must also be int. It looks like T is the "base class" of string and int, using MyList

< T>

(equivalent to client-side code), T can be either string or int, or any other type that meets the constraints, but we don't know anything about it at design time.

Do you also think this is a manifestation of polymorphism?

Let's take a look at the very classic Swap.

< T>

An example of.

Public class Swapper {private static void Swap

< T>

(ref T o1, ref T O2) {T temp = o1; o1 = O2; O2 = temp;}}

Swap

< T>

Generic methods are like encapsulating N non-generic Swap methods, such as Swap (ref int o1, ref int o2), Swap (ref string o1, ref string o2), and so on. With the support of the type inference feature, you can even use generic methods as if they were non-generic methods. Parameter T reflects different parameter forms to some extent, so we have reason to think that generic type T reflects the polymorphism of parameters.

Delegation reflects the polymorphism of the method.

A delegate is an encapsulation of all methods that have the same parameters and return values. As long as methods have the same parameter list and return values, delegates think they belong to the same "type" of methods and can be added to the same delegate list.

Public delegate void FooDelegate (List

< string>

List, string str); public class DelegateTest {public void AddToList (List)

< string>

List, string strToAdd) {list.Add (strToAdd);} public static void PrintIfContains (List

< string>

List, string strToCheck) {if (list.Contains (strToCheck)) Console.WriteLine ("The list contains" + strToCheck);}} public class Program {public static void Main () {List

< string>

List = new List

< string>

(); list.Add ("Kirin"); DelegateTest delegateTest = new DelegateTest (); FooDelegate fooDelegate = new FooDelegate (delegateTest.AddToList); fooDelegate + = new FooDelegate (DelegateTest.PrintIfContains); fooDelegate (list, "Kirin .NET"); Console.ReadLine ();}}

In the above example, the FooDelegate delegate encapsulates the parameter as List

< string>

And string, and there is no method to return a value. Any method that meets the above constraints is treated equally in FooDelegate. For example, the internal implementations of AddToList instance methods and PrintIfContains static methods are completely different except that the parameter list is the same as the return value, but they can be added to the same delegate list. That is, the same delegate can define and call different methods (different implementations with the same constraints).

Do you also think that this is a manifestation of the polymorphism of the method?

Summary

The polymorphism we usually talk about refers to the subclass's rewriting (virtual method) or overwriting (non-virtual method) of the parent class method, which is too narrow. The powerful features of .NET can achieve polymorphism that cannot be achieved in other languages.

The above is the core concept of polymorphism in the OO world. Net, have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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