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

Detailed introduction of C# polymorphic knowledge points

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "detailed introduction of C# polymorphic knowledge points". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "detailed introduction of C# polymorphic knowledge points"!

Catalogue

I. C# Polymorphism

Second, static polymorphism

Third, function overloading

IV. C # operator overloading

1. The realization of operator overloading

2. Overloaded and non-reloadable operators

5. Dynamic polymorphism

Preface:

Anyone who has studied programming knows that there is a term called "OOP thinking"-"object-oriented programming" (Object Oriented Programming,OOP) is a computer programming architecture. One of the basic principles of OOP is that a computer program is composed of a single unit or object that can act as a subroutine. OOP achieves three main goals of software engineering: reusability, flexibility and expansibility. In order to realize the whole operation, each object can receive information, process data and send information to other objects: encapsulation, inheritance, polymorphism.

I. C# Polymorphism

Polymorphism is the ability of the same behavior to have many different forms or forms of expression.

Polymorphism means multiple forms. In the object-oriented programming paradigm, polymorphism is often expressed as "one interface, multiple functions". Polymorphism can be static or dynamic. In static polymorphism, the response of a function occurs at compile time. In dynamic polymorphism, the response of a function occurs at run time. In C #, every type is polymorphic because all types, including user-defined types, inherit from Object. Polymorphism is the same interface that uses different instances to perform different operations, as shown in the figure:

In reality, for example, we press the F1 key:

If what currently pops up under the Flash interface is the help document for AS 3

If the current pop-up under Word is Word help

What pops up under Windows is Windows help and support.

The same event occurs on different objects and produces different results.

Second, static polymorphism

At compile time, the connection mechanism between functions and objects is called early binding, also known as static binding. C# provides two techniques to achieve static polymorphism. They are:

Function overload

Operator overloading

Third, function overloading

You can have multiple definitions of the same function name within the same scope. The definitions of functions must be different from each other, either the type of parameters in the parameter list or the number of parameters. Only function declarations with different return types cannot be overloaded.

The following example demonstrates several identical functions Add (), which are used to add different number of parameters:

Example:

Using System;namespace PolymorphismApplication {public class TestData {public int Add (int a, int b, int c) {return a + b + c;} public int Add (int a, int b) {return a + b }} class Program {static void Main (string [] args) {TestData dataClass = new TestData (); int add1 = dataClass.Add (1,2); int add2 = dataClass.Add (1,2,3); Console.WriteLine ("add1:" + add1); Console.WriteLine ("add2:" + add2) }}}

The following example demonstrates several of the same functions print () for printing different data types:

Example:

Using System;namespace PolymorphismApplication {class Printdata {void print (int I) {Console.WriteLine ("output integer: {0}", I);} void print (double f) {Console.WriteLine ("output floating point: {0}", f);} void print (string s) {Console.WriteLine ("output string: {0}", s) } static void Main (string [] args) {Printdata p = new Printdata (); / / call print to print integer p.print (1); / / call print to print floating point p.print (1.23); / / call print to print string p.print ("Hello Runoob"); Console.ReadKey () }}}

When the above code is compiled and executed, it produces the following results:

Output integer: 1 output floating point: 1.23 output string: Hello Runoob

IV. C # operator overloading

You can redefine or overload the operators built into C #. Therefore, programmers can also use operators of user-defined types. An overloaded operator is a function with a special name, defined by the symbol of the operator followed by the keyword operator. Like other functions, overloaded operators have return types and parameter lists.

For example, look at the following function:

Public static Box operator+ (Box b, Box c) {Box box = new Box (); box.length = b.length + c. Thread; box.breadth = b.breadth + c. Breadth; box.height = b.height + c. Height; return box;}

The above function implements the addition operator (+) for the user-defined class Box. It adds the properties of the two Box objects and returns the added Box object.

1. The realization of operator overloading

The following program demonstrates the complete implementation:

Example:

Using System;namespace OperatorOvlApplication {class Box {private double length; / / private double breadth; / width private double height; / / height public double getVolume () {return length * breadth * height;} public void setLength (double len) {length = len;} public void setBreadth (double bre) {breadth = bre } public void setHeight (double hei) {height = hei;} / / overload + operator to add two Box objects together public static Box operator+ (Box b, Box c) {Box box = new Box (); box.length = b.length + c. Read; box.breadth = b.breadth + c. Breadth; box.height = b.height + c.height Return box;}} class Tester {static void Main (string [] args) {Box Box1 = new Box (); / declare Box1, type Box Box Box2 = new Box (); / declare Box2, type Box Box Box3 = new Box (); / / declare Box3, type Box double volume = 0.0 / / Volume / / Box1 detailing Box1.setLength (6.0,7.0,5.0); / / Box2 detailing Box2.setLength (12.0,13.0); Box2.setHeight (10.0); / / Volume of Box1 volume = Box1.getVolume () Console.WriteLine ("Box1 volume: {0}", volume); / / Box2 volume volume = Box2.getVolume (); Console.WriteLine ("Box2 volume: {0}", volume); / / add two objects together Box3 = Box1 + Box2; / / Box3 volume volume = Box3.getVolume () Console.WriteLine ("volume of Box3: {0}", volume); Console.ReadKey ();}

When the above code is compiled and executed, it produces the following results:

Volume of Box1: 210 Box2 Volume: 1560 Box3 Volume: 5400

2. Overloaded and non-reloadable operators

The following table describes the ability of operator overloading in C#:

Example for the above discussion, let's extend the above example to overload more operators:

Example:

Using System;namespace OperatorOvlApplication {class Box {private double length; / / length private double breadth; / / width private double height; / / height public double getVolume () {return length * breadth * height;} public void setLength (double len) {length = len } public void setBreadth (double bre) {breadth = bre;} public void setHeight (double hei) {height = hei;} / / overload + operator to add two Box objects together public static Box operator+ (Box b, Box c) {Box box = new Box (); box.length = b.length + c.length Box.breadth = b.breadth + c.breadth; box.height = b.height + c.height; return box;} public static bool operator = = (Box lhs, Box rhs) {bool status = false If (lhs.length = = rhs.length & & lhs.height = = rhs.height & & lhs.breadth = = rhs.breadth) {status = true;} return status;} public static bool operator! = (Box lhs, Box rhs) {bool status = false If (lhs.length! = rhs.length | | lhs.height! = rhs.height | | lhs.breadth! = rhs.breadth) {status = true;} return status;} public static bool operator (Box lhs, Box rhs) {bool status = false If (lhs.length > rhs.length & & lhs.height > rhs.height & & lhs.breadth > rhs.breadth) {status = true;} return status;} public static bool operator = rhs.breadth) {status = true;} return status } public override string ToString () {return String.Format ("({0}, {1}, {2})", length, breadth, height);}} class Tester {static void Main (string [] args) {Box Box1 = new Box (); / / declare Box1, type Box Box Box2 = new Box () / declare Box2, type is Box Box Box3 = new Box (); / / declare Box3, type is Box Box Box4 = new Box (); double volume = 0.0; / / Volume / / Box1 details Box1.setLength (6.0,7.0,5.0) / / Box2 details Box2.setLength (12.0); Box2.setBreadth (13.0); Box2.setHeight (10.0); / / use overloaded ToString () to display two boxes of Console.WriteLine ("Box1: {0}", Box1.ToString ()); Console.WriteLine ("Box2: {0}", Box2.ToString ()) / / Volume of Box1 volume = Box1.getVolume (); Console.WriteLine ("Volume of Box1: {0}", volume); / / Volume of Box2 volume = Box2.getVolume (); Console.WriteLine (Volume of Box2: {0} ", volume); / / add two objects together Box3 = Box1 + Box2 Console.WriteLine ("Box3: {0}", Box3.ToString ()); / / the volume of Box3 volume = Box3.getVolume (); Console.WriteLine ("volume of Box3: {0}", volume); / / comparing the boxes if (Box1 > Box2) Console.WriteLine ("Box1 is greater than Box2"); else Console.WriteLine ("Box1 is not greater than Box2") If (Box1

< Box2) Console.WriteLine("Box1 小于 Box2"); else Console.WriteLine("Box1 不小于 Box2"); if (Box1 >

= Box2) Console.WriteLine ("Box1 is greater than or equal to Box2"); else Console.WriteLine ("Box1 is not greater than or equal to Box2"); if (Box1

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