Chapter 7 Object Pascal Object-Oriented Design
I. There are two types of Delphi: ① inherit the class of the built-in Delphi class ② fully custom object class
1. inherit from the built-in class of Delphi
Syntax:
Type
Class Name = Class (parent class)
Class Member
End;
Features: ① the classes defined by this method are subclasses of the tobject class. ② The class name is a pointer. As a result, a class object cannot be defined. The pointer of the Class Object is defined and the class constructor is not called during definition. You must assign a value to the pointer before using the pointer. Similarly, you must call the create constructor of the class before using the defined class Object Pointer. If you have any questions, try sizeof (the class name or the variable name defined by the class). The XP + Delphi7 environment will always return 4.
Constructor and constructor: Create constructor. For the destructor, do not directly call destroy. It only deletes the object pointer and does not delete the object entity. Before the program is completely terminated, the memory space of the object entity will be occupied. Use free instead of destroy to delete not only object pointers, but also object objects.
2. Fully custom object class
Syntax:
Type
Class Name = Object (parent class)
Class Member
End;
Feature: When a class definition object of this type is used, the Class Object in C ++ is no longer a pointer. Sizeof returns the class size, not the class pointer size.
Constructor and constructor: No constructor is required. As with record, the occupied space is automatically released when the object leaves its scope.
Ii. encapsulation level of Class Members
PRIVATE: it can only be used in the unit where the class declaration is located. Although the private members of the parent class can inherit from the quilt class, the Child classes cannot access them.
Protected: The subclass is accessible. So it can be used in the unit where the class declaration is located and the unit where the subclass declaration is located, and not in other places.
Public: Objects of classes and subclasses are available.
Published: the visibility of published is the same as that of public. The difference is that the object Monitor of Delphi can directly display members in this region, and the difference comes from the rtti mechanism.
Antomated: This reserved word is useful when an automated server is used.
For a class member whose names start with no reserved words, its visibility refers to the previous member. If the first member in the class does not contain reserved words, if the class itself or its parent class uses the "{$ m +}" command to compile, the first member is a published member by default; otherwise, the first member is a public member.
Change the encapsulation level of a member variable: You can re-declare the Member to change the encapsulation level of the parent class member variable in the subclass. But it is better to increase its visibility rather than reduce its visibility.
Iii. definition and implementation of Class Members
Class Members include fields, methods, and attributes. The difference between a property and a field is that some methods of reading or modifying its internal data are related to the property. When declaring a field, if it is an attribute, you must add the reserved word "property" before the attribute name and write the association method. For example, property MAX: integer read fmax wirte setmax default 100; no default value is available.
Subclass member's existing method: The subclass only copies the fields, attributes, and method declarations of the parent class. For the method definition and implementation part of the parent class, the original copy of the parent class is kept, therefore, when the object entity of the subclass uses methods inherited by the parent class, the implementation Part of the member functions of the parent class is aroused Based on the called member function name.
Member functions:
Override, hide, and overload: Define rules and use rules (rules can be classified into applicable conditions and usage methods ).
1. Definition rule of override: the virtual or dynamic method of the ancestor class can be overwritten by the quilt class. The virtual attributes of the ancestor class and the override of the subclass are added only during declaration, but not in implementation. The Declaration (method name, parameter, and return value) of the method to be overwritten must be exactly the same as that of the ancestor class.
Override usage rules: Causes dynamic binding.
Definition rules of pure virtual functions: "virtual; abstract;" or "dynamic; abstract;" is added after the member function declaration, and there is no function implementation part. All pure virtual functions must be override in the subclass.
2. Hide definition rules: the subclass contains functions with the same name as the parent class function, and this function does not include overload.
Hide usage rules: dynamic binding cannot be used. That is, when the parent class Pointer Points to a subclass object, the method with the same name as the parent class in the subclass cannot be called.
3. Definition rules of overload: functions with the same parameters and different functions in the same class can constitute heavy loads. The overload reserved word is added after the function declaration, so that the overload is not added to the implementation area.
4. Static binding and dynamic binding: static binding determines the code of the function call during compilation. Dynamic binding refers to calling the corresponding method based on the actual type of the object during the program running.
A member function without any modifier in the class. It is called a static method. For example, procedure draw, which has been bound to its implementation method during compilation.
This function is used for virtual member functions, and the subclass override indicates that this member function will be dynamically bound during compilation.
4. Introduction to self, as, is, sender, parent, owner, and inherited
1. Self is the same as this. Can be considered as the alias of this object.
2. As: the object of the current class or descendant class can be forcibly converted to itself or its ancestor class, and then the class members of the ancestor class can be called. Syntax: object as class if the object is an ancestor class of the class or a class without an inheritance relationship, it will cause an exception during compilation or runtime. An exception occurs during running. The following figure shows the Source: put a button and an edit button on the form. In the Click Event of the button, enter showmessage (sender as tbutton). Caption). The operation is successful. Set the Click Event of the Edit Control to the Click Event of the button. This exception is triggered when you click the Edit Control.
3. Is: used to determine whether the object of the left operand belongs to the right operand type. Syntax: object is class; Return Value: boolean type. If the object is a class on the right or its subclass, true is returned; if the object is its ancestor class, false is returned. If there is no inheritance relationship, the compilation is incorrect.
Both as and is follow the rule that men are human and not necessarily men. Subclass object is/as parent class.
4. Sender: Multiple controls in Delphi can share the same method. For example, the click events of button1, button2, and button3 share the clicks of button1. The sender parameter of the click method is used to specify which button is currently clicked. In short, sender is the handle used to indicate the control that responds to this event.
5. Parent: read/write attributes. The parent of a component is the parent class that owns the component. The parent of a widget must be a window control component. a widget usually has the parent attribute, which is used to set the parent window.
After the parent class of the component is destructed, the component's appearance cannot be seen on the screen, but it does not indicate that the component has been destructed. If the parent and owner of the component are the same component at that time, it will be destructed.
6. Owner: read-only. When the owner analyzes the structure, the components it contains are also analyzed. The owner of all controls on a form is the form.
DOCK: Set docksite = true for a window to have the dock attribute; set a control to have the dock attribute: set its dragkind = dkdock; dragmode = dmautomatic; then, you can drag the control to the window that sets the property to be docked.
7. inherited: Used in the implementation area of a method. When you set the implementation content of a method of the subclass, if it is more code than the method of the parent class, you only need to write the identifier of the member function of the inherited + parent class. It is equivalent to introducing the Code of the parent class into the subclass, so you can write less code. Constructor for introducing the parent class: first, it must be introduced to the constructor of the Child class; second, it only needs to write inherited.
5. static member functions
Delphi's member functions are equivalent to static methods in other programs. For example, both C ++ and Java add static before member functions. Static in C ++ and Java can be used for member variables and member functions. A member is defined as a class member rather than a member of an object. Static is used to define class variables or class methods. Otherwise, if static is not added, it is used to define object variables or object methods. Delphi does not support static data members.
When defining a class method, you must add "class" before the method name. Note that the declaration and implementation must be added. Although there is a self keyword in a class method, it cannot access member variables, attributes, and general methods. It can be a class method or constructor. All the code is shared with the set "class method" at the beginning ". Object methods exist in such object objects. Each object has its own object method, which points to the object method of the shared program segment, and can be called by self.
Simulate static data members: Although Delphi does not support static members, this can be done using a simulated method. Compile the command to implement this function. Constants defined by the const keyword after {$ J +} can be modified. constants defined by the const keyword after {$ J-} cannot be modified.
The following describes how to use the number method as a static data member.
Tstaticmethod = Class class function number (const nval: integer = 0; const ischange: Boolean = false): integer; end; Class function tstaticmethod. number (const nval: integer; const ischange: Boolean): integer; const {$ J +} data: integer = 0; {$ J-} begin if (ischange) Then data: = nval; Result: = data; end; Procedure tform1.button2click (Sender: tobject); begin tstaticmethod. number (100, true); showmessage (intto STR (tstaticmethod. Number); // If {J ++} and {J-} are not used, 0 is displayed. End;
Chapter 8 Exception Handling
For example, if a school has its own operating rules and school rules, students or teachers who violate the rules will be subject to corresponding rules. There are many school rules, and any violation will be punished. With the development of society, school rules will naturally add new rules.
Exception Handling is to anticipate and handle exceptions that may occur in the program to avoid termination of the program due to unhandled exceptions. An exception is equivalent to a student or teacher who violates the rules, and an exception handling is a penalty imposed by the Office of Academic Affairs according to the school rules.
I. Classification of exceptions
Just as there are both school rules and new school rules, exceptions can also be divided into built-in exceptions in Delphi and custom exceptions. Here, we will list the existing school rules.
1. built-in exception classes in Delphi: They all inherit from the exception class and are defined as "E" in the "sysutils" file.
Built-in exception classes provided by Delphi:
2. custom exception classes
There is a difference between an exception class and a general class customization: it must inherit the built-in class exception or a subclass.
Ii. Methods for triggering exceptions
Does triggering an exception violate the school rules. There are two ways to trigger an exception: Automatic Program triggering and self-triggering. Any method that triggers an exception requires exception handling.
Self-triggered is triggered using the Raise command. Syntax: Raise exception object entity. Note that the Raise command must be used in try... Using T or try... Finally zone.
Iii. Exception Handling
The Office of Academic Affairs handles the mistakes of students or teachers. The school penalty can be divided into two types according to the severity. Exception Handling is also divided into two types: Try... Else T... End and try... Finally... End.
1. Try... Finally... End: first execute try... Finally block. When an exception occurs, the block jumps out and runs finally... End block, and then automatically capture the triggered exception and display the information. If no exception occurs, the task is executed sequentially until the end.
2. Try... Else T... End: Try... When there is no exception in the block T, do not execute the block T... End Block; when an exception occurs, Jump directly to begin T... The end block starts execution. Else T... The end block is used to handle exceptions.