Get Object Data from a class Procedure or Function?
Is there a way to get Object data from开发者_开发问答 its class procedure or function without instantiate it?
You seem to have gotten it wrong:
- Classes are specification on how data is layed out in memory, including code, but no data.
- Objects are instances, meaning that they are data in memory, associated with a type.
- Class methods are methods that have access to class information, but which do not have access to data or instances. This way, they can be called without instantiation.
Without instantiation, there is no data, and you cannot access data if it's not there.
I'm not sure this is what your talking about but...
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, these are all valid examples of class methods. Anything else is not valid as you can't access any structures, variables or non-classed methods of an object with out instantiating it.
To add to Ryan's answer, you can call the class functions without instantiating objects such as this:
var
MyInt: Integer begin
begin
MyInt := TMyClass.a;
Try to use something like that:
fClass := TComponentClass(GetClass(fNode.NodeName));
fControl := TControl(fClass.NewInstance);
fControl.Create(...)
精彩评论