Relive the Delphi: Object-oriented

Source: Internet
Author: User

Any language, as long as the "encapsulation, inheritance, polymorphism," the three basic capabilities, regardless of its way of implementation is direct or tortuous, complex or concise, it can be called "Object-oriented" language.

Delphi's rapid popularity in the year was based on its RAD rapid development to attract programmers ' attention. This is undoubtedly its most attractive advantage, but it also makes people mistakenly think that Delphi is only the advanced version of VB, thus ignoring its object-oriented features.

In fact, pacscal development to Delphi, has fully equipped with all the characteristics of object-oriented: Allow custom classes, classes can inherit (single inheritance), allow method overloading/Overwrite, can define interfaces, classes can implement interfaces, allow to define static methods (i.e. class method), virtual method, abstract class ... Wait, do you still doubt Delphi's object-oriented capabilities for a friend who is biased by Delphi?

Here are some demo code:
1. First define a base class Tpeople

unit upeople;InterfacetypeTpeople=class(TObject)Private_name:string;//Private Member Definition    procedureSet_name (Value:string);//the Set method definition for the Name property    functionGet_name:string;//The Get method definition for the Name property  protected    functionGet_sex:boolean;Virtual;Abstract;//defines abstract virtual methods, which are implemented by subclasses       Public     PropertyName:string ReadGet_nameWriteSet_name;//define the Name property     PropertySex:booleanReadGet_sex;//defines the sex read-only attribute (this property is not implemented, but is passed to the subclass to implement)    class functionTostring:string;//class method, a bit like the static Statics method in C #    procedureShowName;//Common instance MethodsEnd;//Implementation PartImplementation    procedureTpeople.set_name (Value:string); begin_name:=value; End; functionTpeople.get_name:string; beginResult:=_name; End; class functionTpeople.tostring:string; beginResult:='This is a people Class'; End; procedureTpeople.showname; beginWriteln ('Name:'+_name); End; End.

2. Define a subclass Tman

unit Uman;   Interfaceusesupeople; typeTman=class(tpeople)Constructor Create(Name:string);Overload;//Overloaded Constructors    Private_sex:boolean; protected    functionGet_sex:boolean;Override;  Public    functionTostring:string;//instance Method  End;Implementation  ConstructorTman.Create(Name:string);//Note the wording: Do not add overload keyword when implementing  begin     inherited Create; _sex:= true;//male mandatory defined as trueSelf.name: =name; End; functionTMan.Get_Sex:Boolean; beginResult:=_sex; End; functionTman.tostring:string; beginResult:='This is the ToString method in Tman'; End; End.

3. Another sub-class Twoman

unit Uwoman;InterfaceusesUpeople,uicook; typeTwoman=class(Tpeople,icook)Constructor Create(Name:string);Overload ; Private_sex:boolean; protected    functionGet_sex:boolean;Override;  Public    procedureCook;//method implementation definition of the interface    procedureShowName;Overload; //if class is inherited from TObject and wants to implement an interface, the following three function must be implemented,    //If you want to be lazy, change the upeople to inherit from Tinterfacedobject, you can dispense with this step.    function_addref:integer;stdcall; function_release:integer;stdcall; functionQueryInterface (ConstIid:tguid; outOBJ): HResult;stdcall; End;Implementation  functionTwoman._addref:integer; beginResult:=-1; End; functionTwoman._release:integer; beginResult:=-1; End; functionTwoman.queryinterface (ConstIid:tguid; outObj): HResult; ConstE_nointerface= $80004002; begin      ifGetInterface (Iid,obj) ThenResult:=0      ElseResult:= -1;{E_nointerface}  End; ConstructorTwoman.Create(Name:string); begin     inherited Create; _sex:=false; Self.name:=name; End; functionTWoman.Get_Sex:Boolean; beginResult:=_sex; End; procedureTwoman.showname; beginWriteln ('women always like to do some tricks, so overload the ha.')  End; procedureTwoman.cook; beginWriteln ('because I implemented the Icook interface, so I would cook:)')  End;End.

Note that this subclass of Twoman implements the interface Icook, which is defined as follows:

4.ICook interface

Unit Uicook; Interface type Interface// define an interface      procedure Cook; //   end; Implementation End.

5. Put the test in the consoleapplication:

 ProgramClassdemo;{$APPTYPE CONSOLE}usesSysutils, Upeopleinch 'Upeople.pas', Umaninch 'Uman.pas', Uwomaninch 'Uwoman.pas', Uicookinch 'Uicook.pas';varapeople:tpeople;  Aman:tman;  Awoman:twoman; Acook:icook;beginapeople:= Tpeople.Create; Apeople.name:='Jimmy.yang';  Writeln (Apeople.name); Writeln (tpeople.tostring);//calling a static methodApeople.showname; Writeln ('----------------------------------------'); AMan:= Tman.Create('Yang Junming');  Writeln (Aman.name);  Writeln (Aman.sex); Aman.showname; //by inheriting the methodWriteln (aman.tostring);//object Methods in an Tman instanceWriteln ('----------------------------------------'); Awoman:= Twoman.Create('Little Dragon Girl');  Writeln (Awoman.name);  Writeln (Awoman.sex); Awoman.showname; //call the overloaded methodAwoman.cook;//ways to implement interfacesWriteln ('----------------------------------------'); Acook:= ICook (Awoman);//classes can also be converted to interfacesApeople.free; Apeople:= Twoman.Create('Cui Hua');//also allows subclasses to be created to get the parent classApeople.showname;  Awoman.free; Awoman:=Twoman (apeople);  Awoman.cook; READLN;End.

Operation Result:
Jimmy.yang
This is a people Class
Name: Jimmy.yang
----------------------------------------
Yang Junming
TRUE
Name: Yang Junming
This is the ToString method in Tman
----------------------------------------
Little Dragon Girl
FALSE
Women always like to do some tricks, so overload the ha.
Because I implemented the Icook interface, so I would cook:)
----------------------------------------
Name: Cui Hua
Because I implemented the Icook interface, so I would cook:)

Relive the Delphi: Object-oriented

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.