In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand C# Polymorphism". The content of the explanation is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "how to understand C# Polymorphism".
The connotation of C# polymorphism is actually through inheritance, a class can be used as multiple types: it can be used as its own type, any base type, or 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.
Understanding of the concept of C# Polymorphism
When a derived class inherits from a base class, it gets all the methods, fields, properties, and events of the base class. To change the data and behavior of the base class, you have two options: you can replace the base member with a new derived member, or you can override the virtual base member.
Replacing a member of the base class with a new derived member requires the new keyword. If the base class defines a method, field, or property, the new keyword is used to create a new definition of the method, field, or property in the derived class. The new keyword is placed before the return type of the class member to be replaced. For example:
An example of the concept of C# polymorphism
Public class BaseClass {public void DoWork () {} public int WorkField; public int WorkProperty {get {return 0;} public class DerivedClass: BaseClass {public new void DoWork () {} public new int WorkField; public new int WorkProperty {get {return 0;}
When the new keyword is used in a C# polymorphic instance, the new class member is called instead of the base class member that has been replaced. These base class members are called hidden members. If you cast an instance of a derived class to an instance of a base class, you can still call a hidden class member. For example:
DerivedClass B = new DerivedClass (); B.DoWork (); / / Calls the new method. BaseClass A = (BaseClass) B; A.DoWork (); / / Calls the old method.
In order for an instance of a derived class to fully succeed a class member from a base class, the base class must declare the member virtual. This is achieved by adding the virtual keyword before the member's return type. The derived class can then choose to use the override keyword instead of new, replacing the base class implementation with its own implementation. For example:
Public class BaseClass {public virtual void DoWork () {} public virtual int WorkProperty {get {return 0;} public class DerivedClass: BaseClass {public override void DoWork () {} public override int WorkProperty {get {return 0;}
In the concept of C# polymorphism, fields cannot be virtual, only methods, properties, events, and indexers can be virtual. When a derived class overrides a virtual member, the member is called even if an instance of the derived class is accessed as an instance of the base class. For example:
DerivedClass B = new DerivedClass (); B.DoWork (); / / Calls the new method. BaseClass A = (BaseClass) B; A.DoWork (); / / Also calls the new method Thank you for your reading. The above is the content of "how to understand C# Polymorphism". After the study of this article, I believe you have a deeper understanding of how to understand C# Polymorphism, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.