C # Quick (5) full text

zhaozj2021-02-11  163

-------------------

Delegate

-------------------

Delegate let us store a function to store in a variable. In C , this is similar to the function pointer defined using typedef, which usually uses a function pointer.

The keyword that is delegated to use is delegate. Hey, you will understand what is entrustion:

Example:

Delegate Int Operation (int Val1, int val2);

Public Int Add (int Val1, Int Val2)

{

RETURN VAL1 VAL2;

}

Public int subtract (int Val1, int Val2)

{

Return VAL1- VAL2;

}

Public void perform ()

{

Operation Oper;

Console.writeline ("Enter or -");

String Optor = console.readline ();

Console.WriteLine ("Enter 2 Operands");

String opnd1 = console.readline ();

String Opnd2 = console.readline ();

INT VAL1 = Convert.TOINT32 (OPND1);

INT VAL2 = Convert.TOINT32 (OPND2);

IF (Optor == " "))

Oper = new operation (add);

Else

Oper = New Operation (Subtract);

Console.writeLine ("Result = {0}", Oper (VAL1, VAL2));

}

-------------------

Inheritance and polymorphism

-------------------

C # only allows for inheritance, multi-inheritance is to be implemented by an interface.

Example:

Class Parent {

}

Class Child: Parent

-------------------

Virtual method

-------------------

In addition to achieving the virtual method in subclasses, the virtual method implements the polymorphic concept C # with C . The parent class uses the same Virtual keyword. Use an Override keyword from each class of overloading virtual methods.

Class Shape

{

Public Virtual Void Draw ()

{

Console.writeline ("Shape.DRAW");

}

}

Class Rectangle: Shape

{

Public Override Void Draw ()

{

Console.writeline ("Rectangle.DRAW");

}

}

Class Square: Rectangle

{

Public Override Void Draw ()

{

Console.writeline ("Square.Draw");

}

}

Class mainclass

{

Static void

Main

(String [] ARGS)

{

Shape [] SHP = New Shape [3];

Rectangle Rect = New Rectangle ();

SHP [0] = New Shape ();

SHP [1] = Rect;

SHP [2] = New Square ();

SHP [0] .draw ();

SHP [1] .draw ();

SHP [2] .draw ();

}

}

Output T:

Shape.draw

Rectangle.draw

Square.draw

-------------------

Use "New" to hide the parent method

-------------------

You can define a subclass into a new method version, hide the version in the base class. You can define a new version using the New keyword. Think about the example below, it is the modified version of the above example. Note When I use the New keyword in the Rectangle class instead of the output of the Override keyword.

Class Shape

{

Public Virtual Void Draw ()

{

Console.writeline ("Shape.DRAW");

}

}

Class Rectangle: Shape

{

Public New Void Draw ()

{

Console.writeline ("Rectangle.DRAW");

}

}

Class Square: Rectangle

{

// Didn't be overloaded here

Public New Void Draw ()

{

Console.writeline ("Square.Draw");

}

}

Class mainclass

{

Static void

Main

(String [] ARGS)

{

Console.writeline ("Using Polymorphism:");

Shape [] SHP = New Shape [3];

Rectangle Rect = New Rectangle ();

SHP [0] = New Shape ();

SHP [1] = Rect;

SHP [2] = New Square ();

SHP [0] .draw ();

SHP [1] .draw ();

SHP [2] .draw ();

Console.writeline ("Using WITHOUT POLYMORPHISM:");

Rect.draw ();

Square sqr = new square ();

SQR.DRAW ();

}

}

Output:

USING POLYMORPHISM

Shape.draw

Shape.draw

Shape.draw

USING WITHOUT POLYMORPHISM:

Rectangle.draw

Square.draw

The polymorphism here does not use the DRAW method of the Rectangle class as a manifestation of the Shape's DRAW method. Instead, it will think this is a different way. Therefore, in order to avoid naming conflicts between the parent class and subclass, we use the New modifier.

Note: You cannot use two versions of one of the following methods below, that is, one version with a New modifier, the other is the version of Override or Virtual modifier. As explained above, I cannot add another method named DRAW to the Rectangle class with the Virtual or Override method. Similarly, in the Square class, I cannot overload the virtual DRAW method of the Square class.

-------------------

Call base class member

-------------------

If the subclass and the base class have the same name, to avoid naming conflicts, access the base class data member and the letter to use a keyword base. In the example below, let's take a look at how to call the constructor of the base class and how to use the data member.

Public Child (int Val): Base (VAL)

{

MYVAR = 5;

Base.myvar;

}

or

Public Child (int Val)

{

Base (VAL);

MYVAR = 5;

Base.myvar;

--------------------------------

In the future supplement:

This article is only a quick overview of a C # language so that you can familiarize with this language. Although I have tried to discuss all the major C # concepts in a concise and comprehensive code, I think there is still a lot to fill in the discussion.

In the future, I will join more commands and concepts that have not been discussed, such as events, and more. I also want to write some Dongdong for Windows programming for C # beginners.

Reference:

Our Most Commonly Known MSDN

Inside C # by Tom Archer

A Programmer's Introduction To C # by Eric Gunnerson

Beginning C # by Karli Watson

Programming C # (O'Reilly)

About the Author

AISHA IS A MASTER OF Science In Computer Science from Quaid-I-Azam Univeristy. She Has Worked in VC 6, MFC, ATL, COM / DCOM, ActiveX, C , SQL, AND SO FORTH. THESE DAYS SHE IS WORKING ON .NET Framework and c #. INSPIRED WITH Nature, She Loves to Seek Knowledge. Sheeeps a free source code and articles web site at http://aishai.netfirms.com.

History

Date posted:

June 13, 2003

转载请注明原文地址:https://www.9cbs.com/read-5913.html

New Post(0)