ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Delphi Records与classes几点不同

2021-10-30 14:33:01  阅读:197  来源: 互联网

标签:Records Delphi record classes they records types


Though records can now share much of the functionality of classes, there are some important differences between classes and records. 
Records do not support inheritance.
Records can contain variant parts; classes cannot.
Records are value types, so they are copied on assignment, passed by value, and allocated on the stack unless they are declared globally or explicitly allocated using the New and Dispose function. Classes are reference types, so they are not copied on assignment, they are passed by reference, and they are allocated on the heap.
Records allow operator overloading on the Win32 platform; classes, however, do not allow operator overloading.
Records are constructed automatically, using a default no-argument constructor, but classes must be explicitly constructed. Because records have a default no-argument constructor, any user-defined record constructor must have one or more parameters.
Record types cannot have destructors.
Virtual methods (those specified with the virtual, dynamic, and message keywords) cannot be used in record types.
Unlike classes, record types on the Win32 platform cannot implement interfaces.

尽管reords目前可以使用许多classes的特性,但是还是有几点重要的不同

  1. records不支持继承
  2. records可以包含variant classes不能
  3. records是值类型,所以在赋值时会直接复制其值。默认在栈stack上分配存储空间,或者使用new,dispose进行分配。classes是引用类型,其赋值时传递是引用。在堆heap上分配
    1. 栈由系统内核管理
    2. 堆程序自动来分配,程序退出时,系统回收。容易造成内存泄露
  4. records在win32平台支持操作符重载
  5. records自动执行一个没有参数的构造函数进行构建。类需要显示的执行构造函数。因为record有一个默认无参的构造函数,所以用户定义的record如果构造函数,其必需有至少一个参数
  6. records不能析构
  7. records中不能使用虚方法
  8. records不能实现接口

标签:Records,Delphi,record,classes,they,records,types
来源: https://www.cnblogs.com/jspdelphi/p/15484962.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有