子类可以访问父类中的受保护函数,但父类不能访问子类中的受保护函数。如何访问子类中的函数?
我想保持两个类尽可能私有。父类是一种形式,只有一次使用实例。子类中的所有函数都是静态的,它从父类继承。
如何从父类访问子类中的非公共静态方法(在另一个单元中)?
编辑:
父类(第一部):
interface
type
TParent = class
public
procedure Initialize;
protected
procedure Test; virtual;
end;
implementation
procedure TParent.Initialize;
begin
Writeln('Initializing');
Test;
end;
procedure TParent.Test;
begin
end;
子类(第二单元):
interface
uses
ParentClass;
type
TChild = class(TParent)
protected
procedure Test;override;
end;
implementation
procedure TChild.Test;
begin
Writeln('Test!');
end;
代码(第三单元):
var c:TParent;
begin
try
c := c.Create;
c.Initialize;
c.Free;
Readln;
end;
输出只是“初始化”。我试图调试它,它没有达到子类。
它永远不会因为你是创建类错到达子类。将您的创建更改为读取C:= TChild.Create,它将起作用。 – skamradt 2009-07-20 19:02:01
您正在实例化该类错误。我想你之前已经被告知过。编译器在你调用`c.Create`时没有警告你?如果你想创建`TChild`的实例,那么你需要调用该类的构造函数*:`TChild.Create`。 – 2009-07-20 19:30:30