在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
http://www.delphibasics.co.uk/Article.asp?Name=Interface
// 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
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DateUtils;
type
// An interface definition
IRecyclable = Interface(IInterface)
// A single function supporting the property
function GetIsRecyclable : Boolean;
// A single property
property isRecyclable : Boolean read GetIsRecyclable;
end;
// Define our Car class
TCar = class(TInterfacedObject, IRecyclable)
private
carName : String;
carAge : Byte;
carIsRecyclable : Boolean;
function GetIsRecyclable : Boolean; // Added for IRecyclable
published
// Car properties
property Name : String
read carName;
property Age : Byte
read carAge
write carAge;
// Added for IRecyclable
property isRecyclable : Boolean read GetIsRecyclable;
// Car constructor
constructor Create(name : String);
end;
// Define our Bicycle class
TBicycle = class(TInterfacedObject, IRecyclable)
private
bikeIsMale : Boolean;
bikeWheelSize : Byte;
function GetIsRecyclable : Boolean; // Added for IRecyclable
published
// Bicycles properties
property isMale : Boolean
read bikeIsMale;
property wheelSize : Byte
read bikeWheelSize
write bikeWheelSize;
// Added for IRecyclable
property isRecyclable : Boolean read GetIsRecyclable;
// Bicycle constructor
constructor Create(isMale : Boolean; wheelSize : Byte);
end;
// The definition of the program form
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
// Constructor implmentation for the car class
constructor TCar.Create(name : String);
begin
// Save the car name and set default age and miles
carName := name;
carAge := 0;
carIsRecyclable :=true // Sets the recyclable indicator
end;
// Car function required for IsRecyclable attribute
function TCar.GetIsRecyclable : Boolean;
begin
Result := carIsRecyclable;
end;
// Constructor implmentation for the bicycle class
constructor TBicycle.Create(isMale : Boolean; wheelSize : Byte);
begin
// Save the passed parameters
bikeIsMale := isMale;
bikeWheelSize := wheelSize;
end;
// Bicycle function required for IsRecyclable attribute
function TBicycle.GetIsRecyclable : Boolean;
begin
// We'll asy that only male bicycles are recyclable
if self.isMale
then Result := true
else Result := false;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
mumsBike : TBicycle;
dadsCar : TCar;
begin
// Instantiate our bike and car objects
mumsBike := TBicycle.Create(false, 24);
dadsCar := TCar.Create('Nissan bluebird');
// Ask if each is recyclable
if dadsCar.isRecyclable
then ShowMessage('Dads car is recyclable')
else ShowMessage('Dads car is not recyclable');
if mumsBike.isRecyclable
then ShowMessage('Mums bike is recyclable')
else ShowMessage('Mums bike is not recyclable');
end;
end.
The ShowMessage shows the following program output:
Dads car is recyclable
Mums bike is not recyclable
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论