在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. eclipse,import,导入项目显示红色叹号

    [场景]eclipse导入项目后,该项目出现红色叹号.打开类文件,会有无数的“The type ... cannot be resolved.”等稀奇古怪的编译错误. [分析]当前eclipse环境中 ...

  2. 保存对象报错with two open Sessions

    purorderService.save(newpur);出现如下 org.springframework.orm.hibernate3.HibernateSystemException: illeg ...

  3. 3.有关于Python列表简述

    一..title() [让所选择的列表元素的第一个字母大写] test = ['no1','No2','No3','No4'] book = "This My " + test[0 ...

  4. 卸载数据盘、更改Inodes

    更改inodes  会格式化数据库,记得先备份 1.fuser -m -v /dev/vdb查看哪些进程正在占用数据库 2.然后kill 掉进程 3.umount /data1/ 4.mkfs.ext ...

  5. 剑指offer例题——二维数组中的查找

    //实现一个函数,将一个字符串的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. public class Solutio ...

  6. debian9 下编译安装tengine2.2.1

    首先下载tengine的源码包,这个大家都会下载吧 wget http://tengine.taobao.org/download/tengine-2.2.1.tar.gz 然后解压缩 tar zxf ...

  7. Python基本模块介绍:sys、platform

    sys模块 常用函数 sys.argv 命令行参数,实现从程序外部向程序传递参数. sys.path 模块搜索路径. sys.platform 获取当前系统平台. sys.version 获取pyth ...

  8. IIS 7配置需要注意的地方,RTX SDK运行必须Enable 32-bit Applications为True

    简单说一下IIS 7的配置里那些需要注意的 首先每个网站都必须运行在特定得程序池上,程序池的配置中,关键的几个如下图: 1. .Net Framework Version : 这个设置的是你项目用到的 ...

  9. Android辅助开发工具说明

    1.aapt(Android Asset Packaging Tool):用于建立zip包(zip.jar.apk),也可用于将资源编译到二进制的assets:2.adb(Android Debug ...

  10. Oracle中PL/SQL的循环语句

    在PL/SQL中可以使用LOOP语句对数据进行循环处理,利用该语句可以循环执行指定的语句序列.常用的LOOP循环语句包含3种形式:基本的LOOP.WHILE...LOOP和FOR...LOOP. LO ...