In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the characteristics of C# polymorphism". In daily operation, I believe that many people have doubts about the characteristics of C# polymorphism. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what are the characteristics of C# polymorphism?" Next, please follow the editor to study!
Before you understand C# polymorphism, first understand what polymorphism is. The same operation acts on different objects, there can be different interpretations, resulting in different implementation results, this is polymorphism. C# polymorphism is achieved by derived classes overriding virtual function methods in the base class.
There are two kinds of C# polymorphism, one is compile-time polymorphism, the other is run-time polymorphism.
◆ compile-time polymorphism: compile-time polymorphism is achieved by overloading. For non-virtual members, the system decides what kind of operation to achieve according to the parameters passed, the type of return and other information at compile time.
◆ Runtime Polymorphism: runtime Polymorphism means that you don't decide what to do according to the actual situation until the system is running. Runtime polymorphism in C # is achieved by overriding virtual members.
Let's take a look at the four concepts involved in C# polymorphism: overloading, overriding, virtual methods, and abstract methods.
The difference between overloading and overwriting of C# polymorphism:
Heavy load
Different versions of the methods defined in the
Public int Calculate (int x, int y) public double Calculate (double x, double y)
Characteristics (two must and one can)
The method name must be the same, the parameter list must be different, and the return value type can be different.
Overwrite
Different implementations of a method are repeatedly defined in subclasses to meet their own needs. Overrides are achieved by using the override keyword. Only virtual and abstract methods can be overridden.
Requirements (same as 3)
(1) same method name
(2) same parameter list
(3) the same return value type
Virtual and abstract methods of C# polymorphism:
Virtual method:
The declaration uses the virtual keyword.
When the virtual method is called, the runtime determines what class the calling object is and calls the appropriate overridden method.
A virtual method can have an implementation.
Abstract method:
Methods that must be overridden by derived classes.
It can be seen as a virtual method without an implementation body.
If a class contains abstract methods, then the class must be defined as an abstract class, whether or not it contains other general methods.
Other introductions to C# Polymorphism:
Yesterday, I suddenly discovered that there are subtle differences in the implementation mechanism of polymorphism between Clover and C++.
If the virtual function is called in the constructor of the base class, the derived class will not be called if the virtual function is called in the constructor of the base class, because the virtual table has not been inherited or modified by the derived class when the base class is constructed.
But if it is a derived class, it will be different. The derived class will still be called in the constructor of the base class. I wonder if anyone knows what the underlying mechanism of C# is?
Well, C++ initializes the base class first, and then initializes the derived types step by step. C # creates the objects at the beginning, and then calls the constructors one by one. The essential difference is that the task of C++ 's constructor is initialization, while C# is not. Any field of the type of C# does not have to be initialized and has a default value, so C# initializes the object before calling the constructor.
Through inheritance, a class can be used as multiple types: as its own type, as any base type, or as any interface type when implementing an interface. This is called polymorphism. Every type in C # is polymorphic. Types can be used as their own types or as Object instances, because any type automatically uses Object as the base type.
Polymorphism is important not only for derived classes, but also for base classes. In any case, using a base class may actually be using a derived class object that has been cast to the base class type. The designer of the base class can predict aspects in its base class that may change in the derived class. For example, the base class that represents a car may contain behaviors that will change when the car under consideration is a minivan or convertible. The base class can mark these class members as virtual, allowing derived classes that represent convertibles and minivans to override this behavior.
At this point, the study on "what are the characteristics of C# polymorphism" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.