Delphi7 (Day 1: writing classes) continued

Source: Internet
Author: User
Program project1; {$ apptype console} uses sysutils, unit1 in 'pas \ unit1.pa'; var P: person; begin P: = person. create; // The abstract method can be instantiated, but the warning statement P appears. setname ('delphi author '); p. setage (0); p. setsex ('Abnormal '); p. speak; p. destroy; // The free and free methods are generally used to determine P. If it is not nil, destroy; readln; end. {1. different interfaces and classes (1) interfaces only have methods, attributes, and no fields. Therefore, all attribute access in the interface must pass the method. (2) methods in the interface are only declared and not implemented. The implementation is completed in the class. (3) the method in the interface has no scope. They are all public, but they are put in different scopes in the class. (4) the method in the interface has no modifier. They can be considered abstract. (5) You cannot create an interface instance. To use an interface, you must implement it in the class and call the interface method through the class. (6) declare and implement all methods of the interface in the class. It is not optional in Class inheritance. (7) When methods in the interface are implemented in the class, virtual/dynamic and abstract can be added to overwrite them in the subclass. {The class inherits from tobject, and the interface inherits from iunknown (=! Interfaces also have an ancestor ).} // Childinterface = interface (parentinterface) // ['{guid}'] // {method list} // end; // The ['{guid}'] (globally unique identifier, globally unique identifier) is called the interface identifier. // The com class can be identified by guid. In this way, the corresponding interface or com class (Instance) can be obtained through guid ). // The Interface ID is not required. In the IDE environment, press Ctrl + Shift + G to generate a guid, or call the cocreateguid function to get the guid //. If the parent interface defines the identifier and its sub-interface is not defined, the identifier is not inherited from the sub-interface. In this case, the sub-interface is considered not identified. // The sysutils unit of Delphi provides the conversion functions stringtoguid and guidtostring between guid and string. /// Excerpt from: http://blog.csdn.net/jiangwzh/article/details/7407086unit unit1; {public: The derived class can directly access the public data in the base class private: the private in the base class is not allowed to access in the derived class, but there are exceptions, if the base class and subclass are in the same unit file, they can be accessed. If they are not in the same unit file, they cannot access protected: in the base class, protected becomes private in the subclass} interfacetype person = Class (tobject) // inherited usage (parent class) Private name: string; age: integer; Sex: string; public constructor create (Name: string; age: integer); overload; // overload is required if two constructors are defined or a function with the same name is reloaded, // If the subclass needs to override the methods in the parent class, the method needs to be modified by virsual // while the subclass needs to use override to modify the method or the constructor create (Name: string; age: integer; Sex: string); overload; // constructor procedure setname (Name: string); function getname: string; Procedure setage (age: integer); function getage: integer; procedure setsex (sex: string); function getsex: string; Procedure speak; procedure run (); Virtual; abstract; // defines the abstract method. This abstract method cannot be implemented in the implementation area, it must be implemented by sub-classes and cannot be declared in the implementation part. // destructor destroy; overload; // destructor end; implementation constructor person. create (Name: string; age: integer); Begin self. name: = Name; self. age: = age; end; constructor person. create (Name: string; age: integer; Sex: string); Begin self. name: = Name; self. age: = age; self. sex: = sex; end; Procedure person. setname (Name: string); Begin self. name: = Name; end; function person. getname: string; begin result: = self. name; end; Procedure person. setage (age: integer); Begin self. age: = age; end; function person. getage: integer; begin result: = self. age; end; Procedure person. setsex (sex: string); Begin self. sex: = sex; end; function person. getsex: string; begin result: = self. sex; end; Procedure person. speak; begin writeln ('writing really slow '); end; {destructor person. destroy; overload; begin inherited destroy; end;} end.

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.