1.接口命名约定I起头,类是T起头的。
- IMyInterface1 = Interface
- function Func1:Integer;
- function Func2:Integer;
- end;
2.接口都是从IInterfacej继承的,若是从根接口继承,则可省略
- {定义继承接口的类}
- TMyClass = class(TInterfacedObject,IMyInterface1,IMyInterface2)
- public
- procedure Proc1;
- procedure Proc2;
- function Func1:Integer;
- function Func2:Integer;
- end;
3.接口成员只能是方法、属性,没有字段
4.接口成员都是公开的
5.接口只有声明,没有实现
6.接口只能从另一个接口继承,但不能从多个接口继承
7.一个类可以实现多个接口
8.实现接口的类一般继承于TInfacedObject
9.接口在用完后会自释放,并同时释放拥有它的类
10.接口的使用
- procedure TForm2.Button2Click(Sender: TObject);
- var
- i1:IMyInterface1;
- begin
- i1:=TMyClass.create;
- i1.Func1;
- i1.Func2;
- end;
接口的属性
1.接口属性的定义
- {此接口声明了一个 Name 属性; 因为接口没有字段, read/write 都只能从方法}
- IMyInterface = interface
- function GetName:string;
- procedure SetName(val:string);
- property name:string read GetName write SetName;
- end;
2.接口属性的实现。通过声明实现接口的类中的字段来存取属性的值
- {类实现的是接口的读写方法, 属性还是属于接口的; 类可以提供一个储存属性的字段}
- TMyClass = class(TInterfacedObject,IMyInterface)
- private
- FName : string;
- public
- function GetName: string;
- procedure SetName(val: string);
- end;