Talk about the classes and objects in Delphi 1. Cannot understand the Championships and understand a few concepts and objects, we can't mention such concepts: class, objects, instances. I personally feel that I can understand this: The object refers to the generals, any entity in the nature can be seen as an object; and the class is a series of types divided by these objects; examples are specifically An object of a certain class. Ok, I don't have to say more these rigons. It's better to come to a "lane" approach, we use Delphicode to explain some concepts of these foreigners to understand the concept of unhealthy understanding of our Chinese: Var ABTN: TButton; Defining ABTN is an object belonging to the TButton class, but ABTN can't say It is an instance because it has not been created, so we said that this is a object that defines an object. If you define an instance, there are fewer fewer fewer sure enough. :) Begin abtn: = tbutton.create (self); // Create a TButton's instance abtn.caption: = 'object'; abtn.free;
2. Object is a pointer to a local channel. From a physical point of view, the object is a piece of address space, the flag of this address space is the class "variable" we defined. So we can see objects as a pointer of a class. Everyone knows that you must initialize the pointer to your pointer. Since the object is a pointer, it must be initialized. How to initialize? Still the initialization of the pointer. For a pointer, there are two methods to perform initialization: (1) Direct assignment var PINT: ^ integer; begin new (pint); PINT ^: = 12; dispose (PINT); END; (2) pointing to others Distributing Var Pint: ^ Integer; I: Integer; Begin I: = 12; PINT: = @ i; end; interesting is that this "pointer" also has two ways to initialize (1) Direct assignment VAR AFORM : TForm; Begin AFORM: = TFORM.CREATE (Self); AFORM.SHOWMODAL; AFORM.FREE; END; (2) Pointing an instance of other assigned spaces VAR AFORM: TFORM; begin AFORM: = Self; AFORM.CAPTION: = 'Know? Why is this'; end; file: // This AFORM and what it points to the Form instance share the same segment address unit, all of which will respond to the AFORM operation file: // Above the FORM instance it corresponds to. Speaking of this, we are very well explained when the process (function) object parameters are passed, like this format: (1) Procedure setEt (VAR Edit: Tedit); begin edit.text: = '11'; end; And (2) Procedure setEdit (Edit: Tedit); Begin Edit.Text: = '11'; END; the effect is the same. (1) In order to pass a TEDIT entity as a parameter reference, (b) is to pass a TEDIT's object "pointer" as a parameter.
3. Categories can be understood as a special data type. We know that the data type can be enforced, and the class can be understood as a data type, then it should also be able to turn. For example, the following code is a button (Button1) click event: (1) procedure tform1.button1click (sender: TOBJECT); VAR Acaption: string; begin acaption: = tbutton (sender) .caption; // sender from TOBJECT Transformation to TButton ShowMessage (Format ('You Clicked' ''% S '"; End; In this code, Sender is a Tobject type object, we mandate it into a TButton type. If you see it unclear, you can refer to our usual data type transformation: (2) procedure tform1.button1click (sender: TOBJECT); var s_str: string; p_str: pchar; begin s_str: = 'i love china!' P_str: = pchar (s_str); s_str: = '; s_str: = string (p_str); showMessage (s_str); end; but in the programming process of the object, it is emphasized that security, such as (one The mandatory type conversion has unsafe. The following code is still written button1.onclick event: (3) procedure tform1.button1click (sender: TOBJECT); begin tcanvas (sender) .brush.color: = CLRED; END; execute, it will be wrong. Isn't this aim of violating the project design of an object? No, it is clear, there should be a specific class forced conversion method, and the method of change (3) is as follows: (4) Procedure TFORM1.BUTTON1CLICK (Sender: Tobject); recomch.color: = CLRED; END; // Use AS to transform, AS can grasp the error and does not affect the normal operation of the program. Speaking of this, I will mention the VB, if you have learned VB, you may think that the array of controls is more cool, especially when writing a program such as a calculator. But what Delphi gives us? The answer is that Delphi can also develop such a program quickly and concise. If you operate: Place an edit and ten button on the form, set the button.caption to '0', '1', '2', ... '9', and then write a button's OnClick event is as follows: (5) Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT); Begin Edit1.Text: = Edit1.Text (Sender As Tbutton) .caption; End; Associate other Button's OnClick events to Button1Click, run the program. Put your hand! This has a prototype of the calculator program. We use Delphi class type to develop the program that is similar to the array of controls in VB is also great! :)
4. Abstract classes and its instance Delphi have a kind of abstract class, you can't really create an instance for it. Such as: TStrings class. As follows: (1) Var strlst: tstrings; begin strlst: = tstrings.create; strlst.add ('i love japan!'); Strlst.free; end; this is wrong. How to construct instances for abstract class such as TSTRINGS? The answer is to use its non-abstract subclass. We know that Tstrings has a TStringList non-abstract subclass. We can do this: (2) Var strlst: tstrings; begin strlst: = tstringlist.create; // With its subclass of constructors, Strlst is subclass strlst.add ('i love china!'); Strlst.free; end; (3) Var strlst: tstringlist; begin strlst: = TSTRINGLIST.CREATE; File: // Don't use the abstract class, completely use its "son" to your business strlst.add ('I love china!'); Strlst.free; end; 5. Class is a package mechanism for data and operation height (1) data package unit unit2;
InterfaType Temployee = Class Private FName: String; PUBLIC Constructor Create; Function GetName: String; Procedure SetName (Aname: String); End; Implementation
{TEMPLOYEE}
Constructor TemPloyee.create; Begin FName: = 'Blazingfire';
Function Temployee.getName: string; begin result: = fname;
Procedure temployee.setname (aname: string); begin fname: = aname;
End. As above, we use a process setName and a function GetName to fully encapsulate the private variable FNAME. uses unit2; procedure TForm1.Button1Click (Sender:: TObject); FName we want to only this operation var AEmployee: TEmployee; begin AEmployee: = TEmployee.Create; AEmployee.SetName ( 'Rose'); // set using SetName FName MessageBox (Handle, Pchar (AEMPLOYE.GETNAME), 'Empoyee', 0); file: // Use getName to access FNAME AEMPLOYE.FREE; END; (2) Operation Package Unit Unit 2;
InterfacePE TDIVISION = Class Public File: // Multi-state makes your program more than "flexibility" Function Getdiv (Num1, Num2: Double): double; overload; function getdiv (Num1, Num2: Integer): Integer; OverLoad ; End; Implementation
{Division}
Function TDIVISION.GETDIV (Num1, Num2: Double): Double; Begin Try Result: = Num1 / Num2; Except Result: = 0; // Provides an ener-meditive processing mechanism, processing divisor is 0 cases; end; function tdiVision.Getdiv (Num1, Num2: Integer: Integer; Begin Try Result: = NUM1 DIV Num2; Except Result: = 0; // Provides an elastic processing mechanism, processing division is 0 cases; end;
End. As the above code we pass the class's polymorphism, the division is handled separately into a whole and non-removal, but also to remove the number of 0, so that the security of the operation is ensured. When you call, we can come like this. : Uses unit2; {$ r * .dfm}
Procedure tForm1.Button1Click (sender: TDIVISION; IVALUE: Integer; Fvalue: Double; Begin Division: = tdivision.create; ivalue: = Division.Getdiv (1, 2); FVALUE: = Division.Getdiv ( 1.0, 2); ivalue: = Division.Getdiv (1,0); FVALUE: = Division.Getdiv (1.0,0); Division.Free; end; 6. Class is a code reusable mechanism ratio in 5 we want Together with this class, a getAdd function can be used as an additional inheritance. You can write as follows: (1) Unit unit2;
interfacetype TDivision = Class public function GetDiv (Num1, Num2: Double): Double; overload; function GetDiv (Num1, Num2: integer): integer; overload; end; type TOperation = Class (TDivision) public function GetAdd (Num1, Num2: Double; End; Implementation
{Division}
Function TDIVISION.GETDIV (Num1, Num2: Double): Double; Begin Try Result: = Num1 / Num2; Except Result: = 0; end;
Function TDIVISION.GETDIV (Num1, Num2: Integer): Integer; begin try results: = Num1 div Num2; Except results: = 0; end;
{TOPERATION}
Function TOPERATION.GETD (Num1, Num2: Double): double; begin result: = Num1 Num2;
Here we inherit a subclass TOPERATION from TDIVISION. TOPERATION You can have a TDIVSION public method Getdiv, and has its own unique method getAdd. This is the method of "fish and bear palms" provided by us. Not bad right. :)