博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Delphi 之Inherited详解
阅读量:5937 次
发布时间:2019-06-19

本文共 3988 字,大约阅读时间需要 13 分钟。

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
  Starts the declaration of a type of object class
  Defines the method used to create an object from a class
  Defines the method used to destroy an object
  Defines a subroutine that returns a value
  Allows a subroutine data type to refer to an object method
  Allows 2 or more routines to have the same name
  Defines a method that replaces a virtual parent class method
  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


转载于:https://www.cnblogs.com/wanqian/p/3156325.html

你可能感兴趣的文章
紫书 例题 11-12 UVa 1515 (最大流最小割)
查看>>
紫书 习题 11-17 UVa 1670 (图论构造)
查看>>
洛谷P1108 低价购买 (最长下降子序列方案数)(int,long long等 范围)
查看>>
大道至简-第五章-心得体会
查看>>
Python编程从入门到实践,个人笔记
查看>>
哈尔滨理工大学第七届程序设计竞赛初赛(高年级组)F - 苦逼的单身狗
查看>>
oracle数据迁移
查看>>
ArchLinux and LXDE and LXDM
查看>>
对拍--from Altf4
查看>>
JavaScript基础6——全选示例
查看>>
JavaScript基础知识总结(三)
查看>>
【python3】爬取简书评论生成词云
查看>>
HttpApplication、HttpContext、HttpModule、HttpHandler
查看>>
Android Service学习
查看>>
【数据库】sql2008卸载和默认实例的删除 ...
查看>>
SDNU 1086.迷宫问题(bfs标记路径)
查看>>
类的高内聚低耦合
查看>>
怎么下载geventwebsocket
查看>>
React文档(十六)refs和DOM
查看>>
电信计费业务:预后融合OCS到底应该实扣还是虚扣?
查看>>