Interface in Delphi

xiaoxiao2021-03-06  72

Jinzhili asked two interfaces. There are two implementation classes. Encourage a problem special to ask. IF1 = interface procedure show (); end; if2 = interface procedure if2show ();

TshiXian = Class (TinterFaceDObject, IF1) Procedure show ();

TTWO = Class (TshiXian, IF2) procedure if2show (); procedure twopriVate ();

Procedure tshixian.show; begin showmessage ('if1 show month');

Procedure TTWO.IF2SHOW; Begin ShowMessage ('ifWshow TTWO'); END;

Procedure TTWO.TWOPRIVATE; Begin ShowMessage ('this is ttwo private month');

The following is called: var if1obj: if1; if2obj: IF2; begin if1obj: = tshixian.create ;1obj.show; // This sentence can also understand if2obj: = if2 (if1Obj); // This sentence is to convert IF2OBJ IF2 if2obj.if2show; // This sentence still calls Tshixian.show; method. What is going on? END;

After the test, Var if1Obj: if1; if2obj: IF2; declares that 2 IF is not two examples of tshixian and TTWO, the IF1 is converted to IF2, and the corresponding same form of Procedure is converted.

If the procedure tform1.button2click (sender: TOBJECT); var if1obj: tstwo; begin if1obj: ttwo; begin if1obj: = tshixian.create; if1obj.show; // This sentence also understands if2obj: = ttwo (if1obj); // This sentence is to convert the IF2Obj to IF2 if2obj.if2show; // This is correct.

Key if2obj.if2show; // This sentence still calls Tshixian.show; method is that this place is not clear.

if1obj is instantiated by tshixian.create. At this time, you will have a memory. So in executing if2obj: = if2 (if1obj);, it is actually assigned the same address to IF2OBJ.

IF2 (if1Obj); just a pointer, a memory address, the address IF1Obj of an OBJ instance of IF2 is determined is a variable of the IF1 type. When the strong type converted into IF2, Delphi did something behind. Strong OBJ is to identify what you specify in the form of TOBJ. There are still some other transformations for non-pointed operations. When the calling OBJ method points to the error, it is generally inconsistent.

If1obj This OBJ has a function pointer (parameter style, return value) as the IF2Obj. Transforming IF1OBJ at all IF2 interface into IF2OBJ, it is dangerous to operate 2, but it is only a pointer, which can only be said that because 2 have a function of a position and offset, there is no error. Happen ...

That is: if2obj.if2show; call the tshixian.show method is the same because the pointer behind is the same. So it will be called.

This problem is originally here.

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

New Post(0)