In Delphi I often see code like this :
TmyClass = class
public
class function getSomething: integer; virtual; abstract;
end;
But what is the purpose of such declaration (ie: class function), because calling TmyClass.getSomething
will always fail as it is not implemented, even if it is implemented in a child class.
It fails if you call TmyClass.getSomething
directly, but it can be useful in combination with metaclasses. It gives you opportunity to define abstract API just like it would on non class functions.
For instance:
TmyClassClass = class of TMyClass;
TmyClass1 = class(TmyClass)
public
class function getSomething: integer; override;
end;
var
c: TmyClassClass;
c := TmyClass1;
c.getSomething;
Of course, you can always use class functions on object instances, so calling getSomething
on TMyClass1
object instance will work, too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With