我在类型库编辑器中为一个接口ITest添加属性Name和方法DoSomething,在接口和接口实现类中会自动添加如下声明:
Itest = interface(IDispatch)
['{0E06D364-58CF-4BA2-AC0E-3AB3F8FA22A0}']
procedure DoSomeThing; safecall;
function Get_Name: Integer; safecall;
procedure Set_Name(Value: Integer); safecall;
property Name: Integer read Get_Name write Set_Name;
end;Ttest = class(TAutoObject, Itest)
protected
procedure DoSomeThing; safecall;
function Get_Name: Integer; safecall;
procedure Set_Name(Value: Integer); safecall;end;现在有两个问题不明白:
1、在实现类中Ttest中为何没有声明property Name: Integer read Get_Name write Set_Name?而只有Get_Name和Set_Name声明,那样的话别的类访问这个类时不是没有Name属性了吗?
2、为何在实现类中自动生成的声明会在protected中而不是public,在protected中的话,那这些方法(例如DoSomeThing)对其他类不就是不可见吗?希望懂的朋友解答一下,谢谢!

解决方案 »

  1.   

    1. Name属性只是一个“快捷方式”,实际上是通过getter和setter访问的。
    2. 客户端是以接口的方式(而不是类)调用ITest,所以可以访问接口里面的任何方法。
      

  2.   

    接口里的声明都是public的
    Ttest继承自Itest 所以也是有Name的
    只是需要一个类型转换
    var
      test:Ttest;
      G:Itest;
    begin
      test:=Ttest.Create;
      G:=t as Itest;
      ShowMessage(IntToStr(G.Name));
      test.free;
    end;
    protected 声明的方法 在他的子类中是可以用的 
      

  3.   

    不好意思写错了
    G:=test as Itest;