Examples of Inherited with and without explicit parent method names
来源:互联网 发布:物业管理app开发源码 编辑:程序博客网 时间:2024/05/01 14:14
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.
// 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
- Examples of Inherited with and without explicit parent method names
- Nginx rewrite URL examples with and without redirect address
- Table of Language Culture Names, Codes, and ISO Values Method [C++]
- Method Names and Non-ARC Compiled Code
- Cannot reduce the visibility of the inherited method from
- Ubuntu_Missing Icons and file names with Xfce
- 51+ Best of jQuery Tutorials and Examples
- links of JDBC tutorial and simple examples
- Examples of Hough Transfer and FFT(MATLAB)
- Examples of VHDL Conversions Numeric_Std and Std_Logic_Arith
- Type conversions with implicit and explicit operators
- Synchronize the height of iframe and parent
- Method of Overriding and Overloading
- java-Cannot reduce the visibility of the inherited method from 父类
- Cannot reduce the visibility of the inherited method from 编译错误
- Java错误提示------------------Cannot reduce the visibility of the inherited method from MouseListener
- Java编译错误:Cannot reduce the visibility of the inherited method from <class>
- Cannot reduce the visibility of the inherited method from Interface解决办法
- IT人喝酒的讲究
- 利用SQL统计时间时怎么去除周末的时间(非工作日时间),我需要的是工作时间.
- WLAN与Bluetooth技术特征
- 扔掉“my”,SAP重新做回自己
- Asp.net 的内置对象
- Examples of Inherited with and without explicit parent method names
- Eclipse+MyEclipse+Struts+Hibernate+MySql动画系列教程下载
- 标准九九乘法表
- 比我长5岁的大哥和大姐
- 用数据库日志文件恢复数据库
- Just do it!
- js 文本框联动
- 转 测试用例的有效维护
- 无数据库日志文件恢复数据库方法两则