In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is C# Polymorphism". In daily operation, I believe many people have doubts about what C# Polymorphism is. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "what is C# Polymorphism?" Next, please follow the editor to study!
Polymorphism is one of the three mechanisms in object-oriented programming, and its principle is based on the rule that a subclass inherited from a parent class can be transformed into its parent class. In other words, where a parent class can be used, the subclass of that class can be used. When many subclasses are derived from the parent class, because each subclass has its own different code implementation, when referencing these subclasses with the parent class, the same operation can show different operation results, which is called polymorphism.
1. Understand what is C# Polymorphism
two。 How to define a virtual method
3. How to overload a virtual method
4. How to use Polymorphism in Program
Another important concept in object-oriented programming is C# polymorphism. At run time, methods in an implementation derived class can be called through a pointer to the base class. You can put a set of objects in an array and then call their methods, in this case, polymorphism is reflected, these objects do not have to be the same type of objects. Of course, if they all inherit from a class, you can put these derived classes in an array. If these objects all have methods of the same name, you can call the methods of the same name for each object. This lesson will show you how to accomplish these things.
1. Listing 9-1. Base class with virtual methods: DrawingObject.cs
Using System; public class DrawingObject {public virtual void Draw () {Console.WriteLine ("I'm just a generic drawing object.");}}
Description
Listing 9-1 defines the DrawingObject class. This is a base class that allows other objects to inherit. This class has a method called Draw (). The Draw () method has a virtual modifier that indicates that the derived class of the base class can overload the method. The Draw () method of the DrawingObject class does the following: output the statement "I'm just a generic drawing object." To the console.
two。 Listing 9-2. Derived classes with overloaded methods: Line.cs, Circle.cs, and Square.cs
Using System; public class Line: DrawingObject {public override void Draw () {Console.WriteLine ("I'm a Line.");} public class Circle: DrawingObject {public override void Draw () {Console.WriteLine ("I'm a Circle.") } public class Square: DrawingObject {public override void Draw () {Console.WriteLine ("I'm a Square.");}}
Description
Listing 9-2 defines three classes. All three classes are derived from the DrawingObject class. Each class has a Draw () method with the same name, and each of these Draw () methods has an overloaded modifier. The overload modifier allows the method to overload the virtual method of its base class at run time on the condition that the class is referenced by a pointer variable of the base class type.
3. Listing 9-3. Program to realize C# Polymorphism: DrawDemo.cs
Using System; public class DrawDemo {public static int Main (string [] args) {DrawingObject [] dObj = new DrawingObject [4]; dObj [0] = new Line (); dObj [1] = new Circle (); dObj [2] = new Square (); dObj [3] = new DrawingObject () Foreach (DrawingObject drawObj in dObj) {drawObj.Draw ();} return 0;}}
Description
Listing 9-3 demonstrates the implementation of polymorphism, which uses the classes defined in listings 9-1 and 9-2. In the Main () method in the DrawDemo class, an array is created, and the array elements are objects of the DrawingObject class. The array is called dObj and consists of four objects of type DrawingObject.
Next, initialize the dObj array, and since the Line, Circle, and Square classes are all derived from the DrawingObject class, these classes can be used as types of dObj array elements. If C# doesn't have this feature, you have to create an array for each class. The nature of inheritance allows derived objects to be used as members of the base class, saving programming effort.
Once the array is initialized, it is followed by a foreach loop to find each element in the array. In each loop, each element (object) of the dObj array calls its Draw () method. Polymorphism is reflected in the fact that at run time, each object's Draw () method is called separately. Although the reference object type in the dObj array is DrawingObject, this does not affect the derived class overloading the virtual method Draw () of the DrawingObject class. In the dObj array, the overloaded Draw () method in the derived class is called through a pointer to the DrawingObject base class.
The output is as follows:
I'm a Line.
I'm a Circle.
I'm a Square.
I'm just a generic drawing object.
In the DrawDemo program, the overloaded Draw () method of each derived class is called. On the * line, the virtual method Draw () of the DrawingObject class is executed. This is because when running to * *, the fourth element of the array is the object of the DrawingObject class.
At this point, the study of "what is C# Polymorphism" is over, hoping to solve everyone's 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.