class function如何知道所在的类
TParent类有个GetInstance的类函数,如何使在TParent中调用时创建TParent对象,如果是在子类TChild中调用,就用返回TChild对象。
TParent=class
protected
constructor Create;virtual;
public
class function GetInstance:TParent;
end;
TParentClass=class of TParent;
TChild=class(TParent)
protected
constructor Create;override;
end;
class function TParent.GetInstance:TParent;
var
c:TParentClass;
begin
c:=当前的类;//如果在对象方法中,可以这样写 c:=TParentClass(self.ClassType),但是在类方法中就不行了
result:=c.create;
end;
调用:
var
parent:TParent;
child:TChild;
begin
parent:=TParent.GetInstance;
child:=TChild(TChild.GetInstance);
end;
目前的实现方法是将GetInstance定义为virtual,TChild中override它,来实现,但每个子类都要实现一次,太麻烦了,有否更好的方法?
TParent=class
protected
constructor Create;virtual;
public
class function GetInstance:TParent;
end;
TParentClass=class of TParent;
TChild=class(TParent)
protected
constructor Create;override;
end;
class function TParent.GetInstance:TParent;
var
c:TParentClass;
begin
c:=当前的类;//如果在对象方法中,可以这样写 c:=TParentClass(self.ClassType),但是在类方法中就不行了
result:=c.create;
end;
调用:
var
parent:TParent;
child:TChild;
begin
parent:=TParent.GetInstance;
child:=TChild(TChild.GetInstance);
end;
目前的实现方法是将GetInstance定义为virtual,TChild中override它,来实现,但每个子类都要实现一次,太麻烦了,有否更好的方法?
...全文
相关推荐
TypeError Function cannot override external function with private visibility解决办法.md
覆盖(Override)指的是在子类(Derived Class)中重新定义父类(Base Class)中已有的方法,以改变或扩展其功能。
IOS使用lua动态生成类 dome
这里我们将编写lua代码来动态生成类。同时,确保你的工程设置正确,让Xcode知道如何处理lua文件。
UVM factory 机制的简单实现(有代码)
**类注册**:在UVM中,每个组件类都需要在工厂中进行注册,以便工厂知道如何创建这些类的实例。在UVM中,通常在类定义中使用`uvm_component_utils`宏来自动完成注册。2.
PHP简单工厂模式
以下是一个可能的代码结构:```php// 基础运算类class MathOperation { abstract public function calculate($num1, $num2);}//
PHP类的自动加载机制实现方法分析
\n"; }}```在另一个文件`test.php`中,如果直接调用`Test1::test()`,则会出现“Class 'Test1' not found”的致命错误,因为PHP不知道`Test1`类定义在何处


语言基础/算法/系统设计
