Delphi: Class Static Methods
在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;
参考资料:
optimization - Does Delphi really handle dynamic classes better than static?
Delphi: Class Static Methods的更多相关文章
- Mongoose 'static' methods vs. 'instance' methods
statics are the methods defined on the Model. methods are defined on the document (instance). We may ...
- Java SE 8 docs——Static Methods、Instance Methods、Abstract Methods、Concrete Methods和field
一.Static Methods.Instance Methods.Abstract Methods.Concrete Methods ——Static Methods:静态方法 ——Instance ...
- Android JNI 学习(九):Static Fields Api & Static Methods Api
一.Accessing Static Fields(访问静态域) 1. GetStaticFieldID jfieldIDGetStaticFieldID(JNIEnv *env, jclass cl ...
- [React] Define defaultProps and PropTypes as static methods in class component
class Toggle extends Component { static propTypes = { defaultOn: PropTypes.bool, on: PropTypes.bool, ...
- Effective Java 01 Consider static factory methods instead of constructors
Advantage Unlike constructors, they have names. (BigInteger.probablePrime vs BigInteger(int, int, Ra ...
- 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 ...
- 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 ...
- Static Fields and Methods
If you define a field as static, then there is only one such field per class. In contrast, each obje ...
- DELPHI学习---类和对象(五篇)
Classes and objects(类和对象) 类(或者类类型)定义了一个结构,它包括字段(也称为域).方法和属性:类的实例叫做对象:类的字段.方法和属性被称为它的部件(components)或成 ...
随机推荐
- 继承标签extend
写页面的时候,整体框架是相同的,只有content区是不同的,所以就有了继承的概念: 在content 里面加一个 {%block content%} {% endblock %} 其他框架的继承: ...
- jquery接触初级----jquery 选择器
css 选择器主要有:元素选择器,ID选择器,类选择器,群组选择器,后代选择器,普通配符选择器等,通过css选择,我们可以很方便的给元素添加样式,使网页看起来更加好看 jquery 选择器也有相似的功 ...
- Nginx缓存配置以及nginx ngx_cache_purge模块的使用
web缓存位于内容源Web服务器和客户端之间,当用户访问一个URL时,Web缓存服务器会去后端Web源服务器取回要输出的内容,然后,当下一个请求到来时,如果访问的是相同的URL,Web缓存服务器直接输 ...
- avalon2学习教程 03数据填充
数据填充是一个模版最基础的功能,直接从JSON(vm)取出数据,放到适当的位置上.在静态模板中,不区分文本与HTML,只看你的字符串是否有< >来决定生成文本节点与元素节点.但MVVM中, ...
- maven ,添加加密算法 apache commons-codec.jar 包
在百度搜索commons-codec.jar maven , http://mvnrepository.com/ 到 maven 库搜索 commons-codec.jar maven .你需要添加 ...
- C语言函数入参压栈顺序为什么是从右向左?
看到有人提问到,在处理printf/cout时,压栈顺序是什么样的?大家都知道是从右往左,也就是说从右往左的计算,但是,这里的计算不等于输出. a++和++a的压栈的区别:在计算时,遇到a++会记录此 ...
- 9.mysql-存储过程.md
目录 创建 创建 -- 创建存储过程 DELIMITER $ -- 声明存储过程的结束符 CREATE PROCEDURE pro_test() --存储过程名称(参数列表) BEGIN -- 开始 ...
- jsonp封装成promise
首先将jsonp通过npm 安装引入js文件中,代码如下 import originJsonp from 'jsonp' export default function jsonp(url, data ...
- pandas 数据结构基础与转换
pandas 最常用的三种基本数据结构: 1.dataFrame: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Data ...
- yum被锁定:Another app is currently holding the yum lock; waiting for it to exit…
yum被锁定无法使用,错误信息截图如下: 解决方法:rm -rf /var/run/yum.pid 来强行解除锁定,然后你的yum就可以运行了