有没有办法从其类过程或函数中获取对象数据而不实例化它?
问问题
600 次
4 回答
2
你好像搞错了:
- 类是关于数据如何在内存中布局的规范,包括代码,但不包括数据。
- 对象是实例,这意味着它们是内存中的数据,与类型相关联。
- 类方法是可以访问类信息但不能访问数据或实例的方法。这样,可以在不实例化的情况下调用它们。
没有实例化,就没有数据,如果数据不存在,您就无法访问数据。
于 2009-11-02T17:47:58.217 回答
0
我不确定这是你在说什么,但是...
type
tmyclasstype = class of tmyclass;
tmyclass = class(TObject)
class function a:integer;
class function b:tmyclass;
class function c:tmyclasstype;
end;
...
class tmyclass.function a:integer;
begin
result := 0;
end;
class tmyclass.function b:tmyclass;
begin
result := tmyclass.create;
end;
class tmyclass.function c:tmyclasstype;
begin
result := tmyclass;
end;
IIRC,这些都是类方法的有效示例。其他任何东西都是无效的,因为如果不实例化对象,您就无法访问对象的任何结构、变量或非分类方法。
于 2009-11-02T17:13:28.980 回答
0
要添加到 Ryan 的答案,您可以在不实例化对象的情况下调用类函数,例如:
var
MyInt: Integer begin
begin
MyInt := TMyClass.a;
于 2009-11-02T17:41:21.163 回答
0
尝试使用类似的东西:
fClass := TComponentClass(GetClass(fNode.NodeName));
fControl := TControl(fClass.NewInstance);
fControl.Create(...)
于 2011-01-20T19:07:11.397 回答