Delphi Object-oriented learning essay three: Overload and override

Source: Internet
Author: User

Bahamut
(reproduced please specify the source and remain intact)
First of all, I want to explain overload alone, why? Because the overload and the object of the association is not big, so, I feel that it is better to put it alone.
As we all know, in the Pascal grammar rule, there cannot be two functions of the same name in the same unit, for example:

functionfunc(): Boolean;function func(constx: Char): Boolean;

This can be a syntax error because of the restriction of the identifier rule. But the problem is, if we need a few functions that are similar in function but with different parameters, then according to the rules of the identifiers, we have to define several different names, but functions that are identical or similar.
For example, suppose I need a function to implement the value of the return parameter minus parameter two, then we will define the function as follows:

functionSubInt(constValue1, Value2: Integer): Integer;functino SubReal(constValue1, Value2: Real): Real;functionSubDouble(constValue1, Value2: Double): Double;implementationfunctionSubInt(constValue1, Value2: Integer): Integer;begin  Result:= Value1 - Value2;end;functino SubReal(constValue1, Value2: Real): Real;begin  Result:= Value1 - Value2;end;functionSubDouble(constValue1, Value2: Double): Double;begin  Result:= Value1 - Value2;end;

We can see that these three functions, in addition to the name of the parameter, are the same as the implementation, which brings some trouble to the consistency of the program. If we can define a function of the same name to deal with these different situations, it will be beneficial to the reuse of code and our memory. Then we can use the overload to complete our needs.

functionSub(constValue1, Value2: Integer): Integer; overload;functionSub(constValue1, Value2: Real): Real; overload;functionSub(constValue1, Value2: Double): Double; overload;implementationfunctionSub(const Value1, Value2: Integer): Integer;begin  Result:= Value1 - Value2;end;functionSub(constValue1, Value2: Real): Real;begin  Result:= Value1 - Value2;end;functionSub(constValue1, Value2: Double): Double;begin  Result:= Value1 - Value2;end;

Thus, a function with the same name and different data type or number of parameters is called the overload function. This is because the compiler not only uses the information of the function name at compile time, but also uses the parameter type and the amount of information, which can be combined to determine the unique identity required to assign the address, usually we call him signature. For example:

Sub(1, 2);     // 调用的是 function Sub(const Value1, Value2: Integer): Integer;Sub(1.0, 2.0); // 调用的是 function Sub(const Value1, Value2: Real): Real;

Here to illustrate, if the same name function, and the parameters are the same, but the return value is different, it is not possible to implement overload.

For overload's instructions, let's go here, override:
As I said earlier, when you derive a new class, you first inherit all the inheritable parts of the parent class, which means that, by default, all non-private elements of the parent class

Some of the class members or methods are already included in the new class.
However, if we want to add new code to a method of a class and do not want to override the original function in the parent class method, we can use override to implement the method's overwrite, and use inherited to inherit (or invoke) the method function in the parent class method:

type  TBaseClass = class(TObject)    // 定义一个新的父类  protected    FMessage: string;  public    constructorCreate();    // 构造方法    procedureMessageBox(); virtual; // 显示字符串  end;  TChildClass = class(TBaseClass)    // 定义一个TBaseClass的派生类  public    procedureMessageBox(); override; // 重载父类中的方法  end;implementationconstructorTBaseClass.Create;begin  inheritedCreate();   // 继承父类中的构造方法Create  FMessage:= ClassName; // 初始化信息字符串为类名end;procedureTBaseClass.MessageBox;begin  ShowMessage(FMessage); // 显示字符串end;procedureTChildClass.MessageBox;begin  FMessage:= FMessage + FMessage;  inheritedMessageBox(); // 继承父类中原有MessageBox方法的原有功能end;

Well, we can write a simple example to invoke:

var  Child: TChildClass;begin  Child:= TChildClass.Create;  Child.MessageBox();  Child.Free;end;

After the

code executes, you should see a dialog box that pops up the "Tchildclasstchildclass" string.
    The code above simply demonstrates the inheritance of class methods, and perhaps you might wonder why Tbaseclass's constructor was not specified with the override keyword. But also can inherited to inherit this method of the parent class, actually does not have to override also can implement inherits (here if you add override instead will have syntax error, because the TObject class's Create is a static method, And static methods are not allowed to be override), then they have what is different? Let's take a look at the following example:

var  Child: TBaseClass;begin  Child:= TChildClass.Create;  Child.MessageBox();  Child.Free;end;

Executing this test code is just as effective as the code above. Then we take the Tbaseclass class method in the MessageBox after the virtual keyword and subclass the override keyword removed, and then take a look, should be seen a popup "Tchildclass" String dialog box.
What is this for? The reason is because the virtual keyword, the following simple explanation of the virtual keyword:

Virtual and Dynamic methods
The virtual keyword is specified as a virtual method, Object Pascal, by default, all methods are static (unlike class methods, of course), and static methods are characterized by the compiler allocating all memory addresses that are specified when a static method is called when the object is created, that is, The compile time specifies the specific method of the call.
The virtual and dynamic methods (virtual methods and dynamical methods) are dynamically allocated at the time of invocation. Their semantics are the same, and the only difference is that they implement methods and invoke methods, which involve Delphi's compilation mechanism.

The Delphi compiler automatically maintains a virtual method table (VMT) for adding virtual methods and a dynamic method table (DMT) for adding dynamic methods.
A virtual method table holds all virtual method pointers that are declared by the class and its base class. Each class has and has only one virtual method table, and of course each virtual method table has a portal. Regardless of whether a class has a virtual method of his own definition, as long as he inherits the virtual method of the ancestor class, he will have his own virtual method table and enumerate all the virtual methods he inherits. Because each class has its own virtual method table, Delphi can use a virtual method table to identify a class, in fact, a class reference is a pointer to the virtual method table of the class.
Because there is a strong correlation between the virtual method tables of the base class and the derived class, the derived class inherits the virtual method of the ancestor class, which means that the preceding part of the virtual method table of the derived class must be the same as the base class. This requirement is difficult to meet when the base and derived classes are not in the same DLL or EXE. Once the base class has changed, the derived class will cause an error if it is not recompiled. In addition, if the class inherits deeply, the virtual method table will be inflated by the inherited virtual methods, resulting in inefficiency.
Dynamic methods can solve this problem, the dynamic method table of each class only maintains part of the dynamic method, that is, the dynamic method table only lists the dynamic method that the class declares, he does not include the dynamic method inherited from the ancestor class. The dynamic method table uses a unique numbering and retrieval system that, when called on a dynamic method, looks up the code for the method in the indexes array in the dynamic method table, and once it finds a code that matches the method, the method pointer calls the method in the corresponding address. If not found, the dynamic method table in the ancestor class is searched. This solves the problem of the expansion of the virtual method table, but it produces the problem of execution efficiency.

In general, virtual methods focus on increasing speed, dynamic methods focus on saving memory, if a method whose descendant classes basically need to override, then the recommended use of virtual, if a method is not often or almost not in the descendant class is override, it is recommended to use dynamic.

Abstract method
The abstract method is what we often call an abstraction, the method can only have a declaration and cannot have an implementation part, generally only provided to the derived class override, so the general abstract keyword and virtual are together. and generally contain abstract methods of the class generally we call him virtual class.

Class method
The class method is somewhat similar to the static method in C, and the class method in Object Pascal is a method that can be called without instantiating an object. Since he does not need to allocate memory space for an object to call him, it means that other data members in the class cannot be referenced in the class method.
The reputation of the class method is as follows:

type  TNewClass = class(TObject)  public    classprocedureAClassFunciton(...): ...;  end;

Thanks to Liu Xiao, no Liu Yan, the article will be ugly a lot, haha ^_^
Cond......

Delphi Object-oriented learning essay three: Overload and override

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.