In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to talk about different polymorphisms in .NET. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.
In .NET, polymorphism 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. Take the most common List as an example, List and List are syntactically identical, except that the type parameters are different, yet 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 {private T [] items; private int size; public void Add (T item) {if (size = = items.Length) {/ / modify capacity} items [size++] = item;}}
If we use MyList, an array of strings will be declared internally, and the argument to the Add method must also be string. If you use MyList, an array of int is declared internally, and the argument to the Add method must be int. It looks as if T is the "base class" of string and int, and when using MyList (the equivalent of client code), T can be either string or int, or some 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 classic example of Swap.
Public class Swapper {private static void Swap (ref T o1, ref T O2) {T temp = o1; o1 = O2; O2 = temp;}}
Swap 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 list, string str); public class DelegateTest {public void AddToList (List list, string strToAdd) {list.Add (strToAdd);} public static void PrintIfContains (List list, string strToCheck) {if (list.Contains (strToCheck)) Console.WriteLine ("The list contains" + strToCheck) }} public class Program {public static void Main () {List list = new List (); 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 a method whose parameters are List and string and returns no 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?
Polymorphic 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. Generics and delegates are listed in this article. Do you agree with the author's view? If you agree, can you give any other examples?
You may feel that this article lacks technical content. Indeed, this article does not contain any advanced technology and ideas, but only some trivial insights in the recent learning process of the author. It will be an honor for me to help you.
The above is how to talk about the different polymorphisms in .NET. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.