• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Delphi之Inherited详解

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

inherited就是调用祖先类的函数,如果不带参数就是默认调用同名函数
如果带参数则表明子类中的函数个数可能比祖先类要多取其中的几个参数传过去
例如
祖先类有个函数 Create(AName:string);
子类有个函数 Create(AName:string;AComponent:TObject);override;
那么子类的Create函数内就可以这样调用祖先类:
procedure TAClass.Create(AName:string;AComponent:TObject);
begin
Inherited Create(AName);
end;

 

 

 
 
Inherited
Keyword
Used to call the parent class constructor or destructor method
1   Create;
  begin
    Inherited;  // Always call at the start of a constructor
    ...
  end;
2   Create(arguments);
  begin
    Inherited Create(arguments);  // Always call at the start of a constructor
    ...
  end;
3   Destroy;
  begin
    ...
    Inherited;  // Always call at the end of a destructor
  end;
 
Description
The Inherited keyword is used to call the parent constructor or destructor method, as appropriate, for the current class. 
 
It is called at the start of a constructor, and at the end of a desctructor. It is not mandatory, but recommended as good practice. 
 
Without parameters, Inherited calls the same named method the parent class, with the same parameters. 
 
You can call a different parent method, if appropriate.
 
Related commands
Class   Starts the declaration of a type of object class
Constructor   Defines the method used to create an object from a class
Destructor   Defines the method used to destroy an object
Function   Defines a subroutine that returns a value
Object   Allows a subroutine data type to refer to an object method
Overload   Allows 2 or more routines to have the same name
Override   Defines a method that replaces a virtual parent class method
Procedure   Defines a subroutine that does not return a value
 
Example code : Examples of Inherited with and without explicit parent method names
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.

unit Unit1;

interface

uses
  Forms, Dialogs, Classes, Controls, StdCtrls;

type
  // Define a parent class, base on TObject by default
  TFruit = class
  public
    name   : string;
    Constructor Create; overload;   // This constructor uses defaults
    Constructor Create(name : string); overload;
  end;

  // Define a descendant types
  TApple = class(TFruit)
  public
    diameter : Integer;
  published
    Constructor Create(name : string; diameter : Integer);
  end;

  // The class for the form used by this unit
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation
{$R *.dfm} // Include form definitions

// Create a fruit object - parameterless version
constructor TFruit.Create;
begin
  // Execute the parent (TObject) constructor first
  Inherited;  // Call the parent Create method

  // Now set a default fruit name
  self.name := 'Fruit';
end;

// Create a fruit object - parameterised version
constructor TFruit.Create(name: string);
begin
  // Execute the parent constructor first
  Inherited Create;  // Call the parent Create method

  // And save the fruit name
  self.name := name;
end;

// Create an apple object
constructor TApple.Create(name: string; diameter : Integer);
begin
  // Execute the parent (TFruit) constructor first
  Inherited Create(name);  // Call the parent method

  // Now save the passed apple diameter
  self.diameter := diameter;
end;

// Main line code
procedure TForm1.FormCreate(Sender: TObject);
var
  fruit  : TFruit;
  banana : TFruit;
  apple  : TApple;

begin
  // Create 3 different fruit objects
  fruit  := TFruit.Create;
  banana := TFruit.Create('Banana');
  apple  := TApple.Create('Pink Lady', 12);

  // See which of our objects are fruits
  if fruit  Is TFruit then ShowMessage(fruit.name  +' is a fruit');
  if banana Is TFruit then ShowMessage(banana.name +' is a fruit');
  if apple  Is TFruit then ShowMessage(apple.name  +' is a fruit');

  // See which objects are apples
  if fruit  Is TApple then ShowMessage(fruit.name   +' is an apple');
  if banana Is TApple then ShowMessage(banana.name  +' is an apple');
  if apple  Is TApple then ShowMessage(apple.name   +' is an apple');
end;
end.
   Fruit is a fruit
   Banana is a fruit
   Pink Lady is a fruit
   Pink Lady is an apple



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Delphi中SendMessage使用说明(所有消息说明)good发布时间:2022-07-18
下一篇:
Delphi7快速调用C#做的webservices(转)发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap