发了上一篇博客.盒子上有朋友认为Class的构造和析构延迟加载.是在Unit的初始化后调用的Class的构造.在Unit的反初始化前调用的Class的析构函数.

为了证明一下我又做了个试验

unit Unit2;

interface

Type
  TClassTest = class
    class constructor create();
    class destructor destory();
  end;

implementation
uses
  Windows;

{ TClassTest }

class constructor TClassTest.create;
begin
  OutputDebugString('class constructor');
end;

class destructor TClassTest.destory;
begin
  OutputDebugString('class destructor');
end;

initialization
   OutputDebugString('Unit initialization');
finalization
   OutputDebugString('Unit finalization');

end.

为了防止编译器因为TClassTest 没有被使用而优化去掉TClassTest .所以我们要用一下TClassTest .

procedure TForm1.FormCreate(Sender: TObject);
begin
  Caption := TClassTest.ClassName;

end;

运行,然后退出.在Event Log窗口中我们见到.

可见类的构造是在Unit初始化前被调用,而类的析构则是在Unit的反初始化后被调用的.

Debug Output: class constructor Process Project1.exe (3940)
Debug Output: Unit initialization Process Project1.exe (3940)
Debug Output: Unit finalization Process Project1.exe (3940)
Debug Output: class destructor Process Project1.exe (3940)

进而再看如果是有派生类,祖先类的情况.

unit Unit2;

interface

Type

TClassTestParent = class
    class constructor create();
    class destructor destory();
  end;

TClassTest = class(TClassTestParent)
    class constructor create();
    class destructor destory();
  end;

implementation
uses
  Windows;

{ TClassTest }

class constructor TClassTest.create;
begin
  OutputDebugString('class constructor');
end;

class destructor TClassTest.destory;
begin
  OutputDebugString('class destructor');
end;

{ TClassTestParent }

class constructor TClassTestParent.create;
begin
  OutputDebugString('Parent class constructor');
end;

class destructor TClassTestParent.destory;
begin
 OutputDebugString('Parent class destructor');
end;

initialization
   OutputDebugString('Unit initialization');
finalization
   OutputDebugString('Unit finalization');

end.
那么结果是:

Debug Output: Parent class constructor Process Project1.exe (3256)
Debug Output: class constructor Process Project1.exe (3256)
Debug Output: Unit initialization Process Project1.exe (3256)
Debug Output: Unit finalization Process Project1.exe (3256)
Debug Output: class destructor Process Project1.exe (3256)
Debug Output: Parent class destructor Process Project1.exe (3256)

可见如果存在类的继承关系则规律是

先父类的构造,然后是子类的构造,最后是单元初始化;先单元反初始化,然后是子类析构,最后是父类析构.

然后再进一步测试:

TClassTestA = class
  public
    class constructor create();
    class destructor destory();
  end;

TClassTestB = class
    class constructor create();
    class destructor destory();
  end;

发现同一个单元内多个不相干的类,似乎是后面的类构造函数是和声明次序相反的.析构则是和声明顺序相同

再进一步测试

改造如下代码:

class constructor TClassTestB.create;
begin
  if(TClassTestA.ClassName = '') then
  else
  OutputDebugString('class B constructor');
end;

发现如果一个类的构造中使用了另外的类,那么构造顺序则是先调用被引用类的构造.

这时想起一个有趣的问题,如果循环使用的话会是什么结果呢.我的猜测是或许编译器不会让通过吧.

class constructor TClassTestB.create;
begin
  if(TClassTestA.ClassName = '') then
  else
  OutputDebugString('class B constructor');
end;

class constructor TClassTestA.create;
begin
  if(TClassTestB.ClassName = '') then
  else
  OutputDebugString('class A constructor');
end;

嘿嘿,出乎我的意料.编译竟然通过了.结果是类构造函数循环使用其他的类的话又变成按声明顺序调用类构造函数,按声明顺序相反顺序调用类的析构函数.

最后的规律就是:

1.类析构顺序总是和类构造顺序相反,类的构造总是在单元初始化前被调用.类的析构器总是在单元的反初始化后被调用

2.无关联的情况下按声明的顺序相反调用类构造

3.优先调父类的类构造函数

4.优先调用在类构造析构器中被使用的其他类的类构造

5.如果在类构造析构器循环使用的话按声明的顺序调用类构造器

可能还有其他规律吧.

同时有个额外发现.如果类有构造器析构器的话Delphi编译器的代码提示会有一个诸如TClassTestA.$ClassInitFlag之类的变量提示出来.

而正常的类则不会有这个提示.当然这个东西肯定是不能在代码中使用的,因为"$"在变量名函数名中都是非法的符号.应该是编译器产生的一些符号表标志被提示出来了.呵呵.

http://blog.csdn.net/wr960204/article/details/4525763

再探Delphi2010 Class的构造和析构顺序的更多相关文章

  1. C++浅析——继承类中构造和析构顺序

    先看测试代码,CTEST 继承自CBase,并包含一个CMember成员对象: static int nIndex = 1; class CMember { public: CMember() { p ...

  2. C++ //继承中构造和析构顺序

    1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 class Base 6 { 7 pu ...

  3. C++语法小记---继承中的构造和析构顺序

    继承中构造和析构的顺序 先父母,后客人,最后自己 静态变量和全局变量在最开始 析构和构造的顺序完全相反 #include <iostream> #include <string> ...

  4. C++ 类成员的构造和析构顺序

    我想对面向对象有了解的童鞋应该不会对类和对象感到陌生吧 ! 对象并不是突然建立起来的,创建对象必须时必须同时创建父类以及包含于其中的对象.C++遵循如下的创建顺序: (1)如果某个类具体基类,执行基类 ...

  5. C++构造和析构的顺序

    C++构造函数和析构函数的顺序 #include <iostream> using namespace std; class CA {public: CA() { cout << ...

  6. C++中的析构顺序和cosnt对象

    1,当程序中存在多个对象的时候,如何确定这些对象的析构顺序? 2,单个对象创建时构造函数的调用顺序(工程经验总结): 1,调用父类的构造过程: 2,调用成员变量的构造函数(调用顺序与声明顺序相同): ...

  7. c++之——派生类的同名成员和函数调用方式及构造析构顺序

    #include<iostream> using namespace std; class Object { public: Object(), b(), c() { cout <& ...

  8. c++再探string之eager-copy、COW和SSO方案

    在牛客网上看到一题字符串拷贝相关的题目,深入挖掘了下才发现原来C++中string的实现还是有好几种优化方法的. 原始题目是这样的: 关于代码输出正确的结果是()(Linux g++ 环境下编译运行) ...

  9. 【再探backbone 02】集合-Collection

    前言 昨天我们一起学习了backbone的model,我个人对backbone的熟悉程度提高了,但是也发现一个严重的问题!!! 我平时压根没有用到model这块的东西,事实上我只用到了view,所以昨 ...

随机推荐

  1. leetcode Minimum Depth of Binary Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  2. SVN版本控制服务器安装与配置

    版本管理在我们日常学习中一般接触不到,因为我们都是一个人在学习与开发一些练习的项目.但是实际中,一般项目都是协同开发的,这样就需要一个版本管理工具,常见的有SVN/CVS/GitHut等...通过它们 ...

  3. 工作备份 build.gradle

    apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion '22.0.0' de ...

  4. Python环境变量配置问题

    安装Python2.7后,在环境变量中加入路径方法如下: 1,设置:右键单击计算机-->属性-->高级系统设置-->环境变量-->Path-->编辑Path-->在 ...

  5. VS Code调试.NET Core

    VS Code调试.NET Core应用遇到的坑 为什么会有”坑“ 博客园里有好多介绍怎么使用VS Code以及调试.NET Core的文章,但是都是基于直接构建Asp.Net Core Mvc单项目 ...

  6. SQL Server 一些重要视图2

    1. sys.dm_tran_session_transactions 为每一个没有关闭的事务返回一行.session_id 可以与sys.dm_exec_connections.session_id ...

  7. ReentrantLock(重入锁)以及公平性

    ReentrantLock(重入锁)以及公平性 标签(空格分隔): java NIO 如果在绝对时间上,先对锁进行获取的请求一定被先满足,那么这个锁是公平的,反之,是不公平的,也就是说等待时间最长的线 ...

  8. 提醒录入BOM更改原因

    应用 Oracle Bill Of   Materiel 层 Level Function 函数名 Funcgtion Name BOM_BOMFDBOM 表单名 Form Name BOMFDBOM ...

  9. 2014第16周三CSS布局再学习摘录

    今天尝试写了下前端页面,费了不少时间,做出的结果仍然惨不忍睹,感觉很简单的几个页面,在现有框架多个样式混杂下就是感觉很不自在随意,晚上回来又看了些div+css方面的基础知识. 1.CSS的class ...

  10. Mac设置

    Mac系统的环境变量,加载顺序为: /etc/profile /etc/paths ~/.bash_profile ~/.bash_login ~/.profile ~/.bashrc