在Delphi中,自Delphi 2007之后,支持static形式的class方法,样式比如:

 type
TMyClass = class
strict private
class var
FX: Integer;
strict protected
// Note: Accessors for class properties
// must be declared class static.
class function GetX: Integer; static;
class procedure SetX(val: Integer); static;
public
class property X: Integer read GetX write SetX;
class function GetInstance: TMyClass;
class procedure StatProc(s: string); static;
end;

即在方法后面加入static关键字,区别于传统的class方法。

一直不明白它与不加static关键字的方法有什么区别,而其官文档又语焉不详,看了又不够透彻,于是写代码验之,大致有点感悟。

结合其官方说法,感觉加static的方法,更像是纯粹的静态方法,其与普通的class方法相比,不同有几点:

1、其方法体内不以用Self关键字

传统class方法可以在方法体内使用Self关键字,而其Self关键字,指的是类而非类实例。比如,它可以调用类的构造函数。

2、其修饰的方法不能再加以virtual来修饰

即它不能被子类改写。

3、类属性之存取方法,须得为static方法

由上几点,感觉class static method主要用处就是做为类属性的访问器,以及纯粹的静态方法。

其它用处,亦未知。

TMyClass类其实现代码如下:

{ TMyClass }

var
FMyClass: TMyClass; class function TMyClass.GetInstance: TMyClass;
begin
if FMyClass = nil then
FMyClass := Self.Create; //这里Self,指的是TMyClass类;在static方法中,无法用Self标记,即它也不能调用类的构造函数
Result := FMyClass;
end; class function TMyClass.GetX: Integer;
begin
Result := FX;
end; class procedure TMyClass.SetX(val: Integer);
begin
FX := val;
end; class procedure TMyClass.StatProc(s: string);
begin
ShowMessage(s);
end;

因为加static没有了Self指针指向,理论上说速度可能更快一些,做如下验证的确如此,但也不明显。

速度测试:

class function GetDigit1(const Value: Integer): Integer;
class function GetDigit2(const Value: Integer): Integer; static; ... { TMC} class function TMC.GetDigit1(const Value: Integer): Integer;
begin
X := X + 1;
Result := Value + Value;
end; class function TMC.GetDigit2(const Value: Integer): Integer;
begin
X := X + 1;
Result := Value + Value;
end;

类调用,后者稍快一点:

var
i, ct: Integer;
begin
ct := GetTickCount;
for i := to do
TMC.GetDigit1(i);
Edit1.Text := IntToStr(GetTickCount - ct); //327ms ct := GetTickCount;
for i := to do
TMC.GetDigit2(i);
Edit2.Text := IntToStr(GetTickCount - ct); //296ms
end;

实例调用:

var
i, ct: Integer;
begin
ct := GetTickCount;
for i := to do
TMC.Create.GetDigit1(i);
Edit1.Text := IntToStr(GetTickCount - ct); //3432ms ct := GetTickCount;
for i := to do
TMC.Create.GetDigit2(i);
Edit2.Text := IntToStr(GetTickCount - ct); //297ms
end;

参考资料:

Methods (Delphi) - RAD Studio

optimization - Does Delphi really handle dynamic classes better than static?

Delphi: Class Static Methods的更多相关文章

  1. Mongoose 'static' methods vs. 'instance' methods

    statics are the methods defined on the Model. methods are defined on the document (instance). We may ...

  2. Java SE 8 docs——Static Methods、Instance Methods、Abstract Methods、Concrete Methods和field

    一.Static Methods.Instance Methods.Abstract Methods.Concrete Methods ——Static Methods:静态方法 ——Instance ...

  3. Android JNI 学习(九):Static Fields Api & Static Methods Api

    一.Accessing Static Fields(访问静态域) 1. GetStaticFieldID jfieldIDGetStaticFieldID(JNIEnv *env, jclass cl ...

  4. [React] Define defaultProps and PropTypes as static methods in class component

    class Toggle extends Component { static propTypes = { defaultOn: PropTypes.bool, on: PropTypes.bool, ...

  5. Effective Java 01 Consider static factory methods instead of constructors

    Advantage Unlike constructors, they have names. (BigInteger.probablePrime vs BigInteger(int, int, Ra ...

  6. Core Java Volume I — 4.4. Static Fields and Methods

    4.4. Static Fields and MethodsIn all sample programs that you have seen, the main method is tagged w ...

  7. Difference Between static and default methods in interface

    I was learning through interfaces when I noticed that you can now define static and default methods ...

  8. Static Fields and Methods

    If you define a field as static, then there is only one such field per class. In contrast, each obje ...

  9. DELPHI学习---类和对象(五篇)

    Classes and objects(类和对象) 类(或者类类型)定义了一个结构,它包括字段(也称为域).方法和属性:类的实例叫做对象:类的字段.方法和属性被称为它的部件(components)或成 ...

随机推荐

  1. 塔式Server 服务器ESXI6.5安装

    参考文献: https://www.cnblogs.com/yufusec/p/9181422.html 第一步: esxi6.5.ios文件的下载 第二步: 通过UltraISO软件 制作启动盘或光 ...

  2. alias 设置别名

    我们在使用Linux中使用较长的命令而且要经常要使用时,总是会使用别名,这里就简单的介绍一下别名alias 指令:alias设置指令的别名 语法:#  alias name='command line ...

  3. struts2默认临时文件更改

    struts的文件上传mutifile会有一个临时文件地址,如果需要使用自己指定临时文件地址需要在struts.xml中设置以下内容. <constant name="struts.m ...

  4. MM-RGV、AGV 、IGV是什么

    RGV.AGV.IGV是什么 智能化物流仓储设备迅速崛起的时代,RGV.AGV.IGV,这三种看似有血缘关系的智能设备,到底有什么不同呢? RGV RGV即“有轨制导车辆”,又叫有轨穿梭小车,是与地面 ...

  5. VB-串口通信详解

    转载:https://blog.csdn.net/dongyue786/article/details/8177047 MSComm 控件通过串行端口传输和接收数据,为应用程序提供串行通讯功能.MSC ...

  6. 跟我一起学Python-day1(条件语句以及初识变量)

    通过练习题来学习条件语句 1,使用while循环输出1 2 3 4 5 6    8  9  10 n=1 while n<11: if n=7: pass else: print(n) n=n ...

  7. 链表有环判断,快慢指针两种方法/合并链表/删除重复元素/二分递归和while

    public static boolean hasCycle(ListNode head) { if (head == null || head.next == null) { return fals ...

  8. The Google File System——论文详解(转)

    “Google文件存储系统(GFS)是构建在廉价服务器之上的大型分布式系统.它将服务器故障视为正常现象,通过软件方式自动容错,在保证系统可用性和可靠性同时,大大降低系统成本. GFS是Google整个 ...

  9. C语言复习:文件操作

    文件操作专题 C语言文件读写概念 文件分类 按文件的逻辑结构: 记录文件:由具有一定结构的记录组成(定长和不定长) 流式文件:由一个个字符(字节)数据顺序组成 按存储介质: 普通文件:存储介质文件(磁 ...

  10. Reduction: the word AT

    Reduction: the word AT Share Tweet Share Tagged With: AT Reduction Study the AT reduction.  There ar ...