1.自定义类,例子如下:
......
type
TMyObject = class
private
SomeValue : Integer;
procedure SetSomeValue(AValue : Integer);
published
property Value : Integer read SomeValue write SetSomeValue;
end;
implementation
procedure TMyObject.SetSomeValue(AValue : Integer);
begin
SomeValue := AValue;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
obj : TMyObject;
begin
obj := TMyObject.Create;
obj.Value := 123;
ShowMessage(IntToStr(obj.Value));
end;
......
2.接口
定义接口如下:
type
IFoo = interface
['{4A8BFAB3-E565-4AC0-97B4-D166B46D4B6F}']
function F1 : Integer;
end;
IBar = interface(IFoo)
['{E6A7FBD6-68E7-43E7-B2E9-717FE9A4115B}']
function F2 : Integer;
end;
定义实现类:
type
TFooBar = class(TInterfacedObject, IFoo, IBar)
function F1 : Integer;
function F2 : Integer;
end;
implementation
{$R *.dfm}
function TFooBar.F1 : Integer;
begin
Result := 1;
end;
function TFooBar.F2 : Integer;
begin
Result := 2;
end;
注意:
如果一个类实现多个接口,但恰好这些接口有同名的方法时,我们需要给同名的方法取别名,例如:
type
IFoo = interface
['{4A8BFAB3-E565-4AC0-97B4-D166B46D4B6F}']
function F1 : Integer;
end;
IBar = interface(IFoo)
['{E6A7FBD6-68E7-43E7-B2E9-717FE9A4115B}']
function F1 : Integer;
end;
type
TFooBar = class(TInterfacedObject, IFoo, IBar)
//aliased methoeds
function IFoo.F1 = F1;
function IBar.F1 = F2;
//interface methoeds
function F1 : Integer;
function F2 : Integer;
end;
implementation
{$R *.dfm}
function TFooBar.F1 : Integer;
begin
Result := 1;
end;
function TFooBar.F2 : Integer;
begin
Result := 2;
end;