Delphi Project of Object

Source: Internet
Author: User

Http://www.cnblogs.com/ywangzi/archive/2012/08/28/2659811.html

In fact, to understand these things, appropriate to learn some disassembly, Windows Memory management mechanism, PE structure, see Levi's VCL architecture analysis can be well understood
Type
Tmyevent = procedure of object;
This is a definition of a data type that defines a type of function that can be used in a class
Different from
Type
Tmyproc = procedure;

Tmyevent and Tmyproc all define a function type, the difference being that Tmyproc can not be used to define events in a class, but tmyevent can.

If you want to know what to ask, then you need to understand what the event type is and what the function type is.

Procedure A ();
Begin
ShowMessage ()
End

Var
Func1:tmyproc;
Func2:tmyevent;

FUNC1: = A;

FUNC1: = A;

What the hell is going on in this sentence?

In a 32-bit Windows operating system, any one process is executed, the work of the operating system basically includes: to open a linear virtual address table for this process (from $00000000 to $FFFFFFFF), and then load the EXE program into the corresponding memory of this address table, And then jump into the EXE program entry point, the rest of the thing is that EXE himself from the entry point to start a step-by-step to do their own things.
program, all functions are compiled to a fixed position, that is, each function has an address corresponding to the function of the call, is actually in the register or stack to prepare parameters, and then jumped into the corresponding address of the function to continue execution.
FUNC1: = A;
This action is done by assigning the address of function A to the FUNC1 variable.


So, what is the completion of the assignment for func2:tmyevent?

Functions in the class are also addressed, and after compilation, the functions in the class are compiled to a fixed address.
However, FUNC2 does not represent a function address, he is a variable of record type
This can be seen by sizeof (tmyevent) the sizeof (tmyevent) return value is 8, and sizeof (TMYPROC) returns 4
In fact, the tmyevent type is a record containing two fields, one of which is code, which is the same as Tmyproc, and the other is data, and he holds an object.

Button2.onclick: = Button1Click;
This assignment, in effect, is to Form1 the object of the form class to the data field, Button1Click the address of the function to the Code field







1.
Tmyevent = procedure of object; Tmyevent is a method type equals String is a data type

If a procedure is defined in a txxx class
Procedure Txxx.test;
Begin
ShowMessage (' 1 ');
End

Then you can assign Txxx.test to Tmyevent (tmyevent: = Txxx.test);
Because the definition of tmyevent has an of object, it can only be assigned to a procedure in a class, not a normal process.

2.
Fonhundred:tmyevent; --fonhundred is a variable whose type is Tmyevent,
is equal to Icount:integer so simple

3.
Property Onhundred:tmyevent Read Fonhundred write fonhundred

Onhundred is a property whose type is also tmyevent to access the value of this property through the fonhundred variable.

Call onhundred and fonhundred are the same when the attribute is not used to access the process (as in the example above)

The following two definitions are often seen in Delphi

Type

Tmouseproc = procedure (X,y:integer);

Tmouseevent = procedure (X,y:integer) of Object;

They look the same, but the actual meaning is different,

Tmouseproc is only a single function pointer type;

Tmouseevent is a function pointer to an object, which is the function/method of an Object/class

The difference is that the class method has a hidden parameter, self, which means that the two parameters are different, so they cannot be converted to each other.

This is why Delphi can be assigned such button1.onclick:=button2.onclick;

But can not be so assigned to the value of Button1.onclick=buttonclick; (ButtonClick is the local function, Button2.onclick is the class method)!


Method type definition: TMethod = procedure of object;

Procedural types allow you to treat procedures and functions as values so can be assigned to variables or passed to othe R Procedures and functions. For example, suppose-define a function called Calc that takes both integer parameters and returns an integer:

function Calc (X,y:integer): Integer;

You can assign the Calc function to the variable F:

var f:function (X,y:integer): Integer;

F: = Calc;

If procedure or function heading and remove the identifier after the word procedure or function, what's left is the name of a procedural type. You can use such type names directly in variable declarations (as-in the example-above) or to declare new types:

Type

Tintegerfunction = Function:integer;

Tprocedure = procedure;

Tstrproc = procedure (const s:string);

Tmathfunc = function (x:double): Double;

Var

F:tintegerfunction; {F is a parameterless function this returns an integer}

Proc:tprocedure; {Proc is a parameterless procedure}

Sp:tstrproc; {SP is a procedure that takes a string parameter}

M:tmathfunc; {M is a function that takes a double (real) Parameterand returns a double}

Procedure Funcproc (p:tintegerfunction); {Funcproc is a procedure whose only parameter is a parameterless integer-valued function}

The variables above is all procedure Pointers-that was, pointers to the address of a procedure or function. If you want to reference a method of a instance object (see Classes and objects), you need to add the words of object to The procedural type name. For example

Type

TMethod = procedure of object;

Tnotifyevent = procedure (sender:tobject) of object;

These types represent method pointers. A method pointer is really a pair of pointers; The first stores the address of a method, and the second stores a reference to the object the method belongs to. Given the Declarations

Type

Tnotifyevent = procedure (sender:tobject) of object;

Tmainform = Class (Tform)

Procedure ButtonClick (Sender:tobject);

...

End

Var

Mainform:tmainform;

Onclick:tnotifyevent

We could make the following assignment. OnClick: = Mainform.buttonclick;

Procedural types is compatible if they have the same calling convention,the same return value (or no return value), a nd the same number of parameters, with identically typed parameters in corresponding positions. (Parameter names do not matter.)

Procedure pointer types is always incompatible with method pointer types. The value nil can is assigned to any procedural type.

Nested procedures and functions (routines declared within other routines) cannot is used as procedural values, nor can pre defined procedures and functions. If you want to use a predefined routine like Length as a procedural value, write a wrapper for it:

function Flength (s:string): Integer;

Begin

Result: = Length (S);

End

Delphi Project of Object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.