[".NET Framework Design"] Chapter 9 Method

zhaozj2021-02-16  70

Chapter 9

First, ???????????? instance constructor

1. The three songs when using the New operator to create an object:

L ???????? Allocate memory for the object

l ???????? Initialization object attach member (method table pointer and SyncBlockIndex)

l ???????? Call the instance constructor to initialize the instance status

When allocating memory, the system is set to zero 0 value, which is a 0 or NULL value when the field initialization is not assigned.

?

Do not call the case of the instance constructor:

l ???????? Call the object.memberwiseclone () method creation instance (allocate memory; initialize additional member; copy the source object byte to the newly created object)

L ???????? During the reverse sequence of objects

2, to avoid excessive constructor code to generate excessive fields, avoiding the initial value of the field when the field is declared, but in the no-argument constructor, in the same value, in other overloaded constructors Call no argument constructor.

?

3, value type instance constructor

l ???????? C # compiler does not automatically call its constructor, you must explicitly call the constructor to work

l ???????? C # compiler does not allow the value type to define no unfolded instance constructors (the following can be described below)

l ???????? Can't assign initiatives to the fields in the declaration, can be performed by defining a refinement

l ???????? must assign all fields in the structured constructor

Second, ???????????? type constructor

1, some limitations of type constructors:

L ???????? Can't bring any parameters

L ???????? Type constructor is always private, can not use other access modifiers

2, the timing of the type constructor is called:

l ???????? The first instance was created, or the first field or member of the type was first accessed.

l ???????? Non-inherited static fields before the first visit

Type constructors are only called once in the life cycle of the type;

3, some restrictions:

l ???????? If the exception is thrown in the type constructor, the type becomes inaccessible, and any field or method to access the System.TypeinitializationException exception.

l ???????? Type constructor can only access the type of static field

l ???????? Type constructor should call the type constructor of the base type, because the static field is not inherited but is compiled when compiling

Third, ???????????? operator overload

1, operator overload

Some restrictions on operator overload in C #:

l ???????? must be declared as public static

l ???????? must have a parameter to be the type of operator

l ???????? Can't change the number of quotes of the operator originally defined

l ???????? If the TRUE operator must also define the FALSE operator, both must return BOOL value.

l ???????? , - operator must return an example of its belongings

L ???????? can be overloaded one yuan operator: , - ,! , ~, , -, true, false

L ???????? Odd Odd Operators: , -, *, /,%,! , ^ (No or), <,>, <<, >>, ==,! =, <=,> =

l ???????? Not allowed to be overloaded: &&, ||, = ,? :, =, - =, / =,% =, | =, ^ =, << =, >> =, in fact, some "duplex operators" automatically generate after the binary operator is overloaded, and Can't explicitly define L ???????? must be a heavy load operator: (==,! =), (<,>), (<=,> =)

l ???????? , - can not distinguish when the operator is overloaded

2, operator overload and language interoperability

The compiler will generate a special name for the overloaded operator, such as the (plus) operator generates the op_addition () method, and adds the SpecialName tag to the definition entry of the method. When a language cannot be overloaded, it is possible to directly define a method with the special name to be called in other languages; or directly call the method with this special name to accommodate the limitations of the operator. If the operator cannot be overloaded in VB, the op_addition () method can be explicitly defined to call in C #; the operator defined in the C # cannot be identified by VB, which can explicitly call the op_addition () method to obtain the same function.

Fourth, ???????????? conversion operator

Some restrictions on the conversion operator:

l ???????? must be public static

l ???????? Must specify the keyword Implicit or ExPlicit, principle is: From this type to other types of Implicit, other types of conversion into this type with explicit, can not use IMPLICI

?

5. ???????????? method parameters

1, reference parameters

L ???????? By default

l ???????? The sign is the parameters of the OUT, do not initialize before the call method, but must be assigned before returning, the parameters that are not initialized cannot be used

l ???????? The flag is the parameters of REF, must initialize before the call method, otherwise trigger compilation error

L ???????? You can use REF or OUT to perform the overload of the method, but cannot be overloaded by distinguishing REF and OUT.

l ???????? The variable (inactive parameters) passed in the reference mode must be identical to the parameter (ginseng) type of the method declared, otherwise the compilation error is triggered.

2, variable number parameters

Specify variable parameter sequences using the params keyword and an object array. Some restrictions:

l ???????? Only the last parameter of the method can use variable number parameters

?

Sixth, ????????????

1, the modulation mechanism of the virtual method

CLR uses two IL instructions to call methods:

u ?????? call? Call a method according to type (ie, reference static type, declaration type)

u ?????? CallVirt ???? Call a method according to the object (ie, the retribution dynamic type, actual type)

The case where the virtual method is used to use Call:

L ????????? base. virtual method (),

l ???????? Sealing type references the virtual method, because there is no need to verify the actual type of sealing type

l ???????? value type, avoid being contained

Use the CallVirt to call the non-virtual method:

l ???????? When the application variable is null, use CallVirt to throw the system.nullreferenceException, and the call does not throw out regardless of the Call or CallVirt call method, there will be an implicit THIS pointer as a method. The first parameter, pointing to the object being operated

2, version of the virtual method:

Use the following example:

Using system;

?

Class Baseclass

{

?????? public void nonvirtualfunc ()

?????? {

????????????? console.writeline ("Non Virtual Func in Base Class);

??????}

??????

?????? public virtual void virtualfunc ()

?????? {

????????????? console.writeline ("Virtual Func In Base Class"); ??

??????}

}

?

Class DevicedClass: Baseclass

{

?????? // If the new key is not used, the compiler will have Warning:

?????? // "DeviceDclass.nonvirtualFunc ()" request keyword

?????? // New because it hides the inheritance member "Baseclass.nonvirtualFunc ()"

?????? public new void nonvirtualfunc ()

?????? {

????????????? console.writeline ("Non Virtual Func in Deviced Class); ?????

??????}

?????? // If you do not add the keyword override or new, the compiler will have Warning:

?????? // "DeviceDclass.VirtualFunc ()" Baseclass.VirtualFunc () "Baseclass.VirtualFunc ()

?????? // "To rewrite the implementation of the current member, add the keyword override. Otherwise, add a keyword

?????? // new.

?????? public override void virtualfunc ()

?????? {

????????????? console.writeline ("Virtual Func In Deviced Class); ?????

??????}

}

?

Class testclass

{

?????? public static void

Main

()

?????? {

????????????? // derived instance call non-virtual and virtual functions

????????????? devicesDClass DC = new devicesDClass ();

????????????? dc.nonvirtualfunc ();

????????????? dc.virtualfunc ();

?

?????????????? // base class instance call non-virtual and virtual functions ?????????????

????????????? baseclass bc = new baseclass ();

????????????? bc.nonvirtualfunc ();

????????????? bc.virtualfunc ();

?????????????

????????????? // Point to the base class reference to the derived class instance call non-virtual and virtual functions

????????????? Baseclass BC1 = DC;

????????????? bc1.nonvirtualfunc ();

????????????? bc1.virtualfunc (); ??????}

}

?

/ *

Use the keyword Override to run the result:

Non Virtual Func in Deviced Class

Virtual Func in Deviced Class

Non Virtual Func in Base Class

Virtual Func in base Class

Non Virtual Func in Base Class

Virtual Func in Deviced Class

* /

?

/ *

Use the keyword new run results on the virtual function

Non Virtual Func in Deviced Class

Virtual Func in Deviced Class

Non Virtual Func in Base Class

Virtual Func in base Class

Non Virtual Func in Base Class

Virtual Func in base Class

* /

It can be seen from the above: New and Override Coordinate the version control in the derived class, which has seen Oeverride only for the Virtual method in Chapter 7, and New is available for non-virtual or virtual methods to implement the same name in the hidden base class. method. Use Override on the virtual function, to rewrite the basic class, there is no hidden, which also implements a polymorphism. We can imagine that NEW uses the CALL instruction to call the static type method, and Override uses the CallVirt command to call the dynamic type method.

I hope this example is helpful to your understanding.

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

New Post(0)