首页 > 其他分享 >delphi property中default的含义

delphi property中default的含义

时间:2024-06-06 15:12:29浏览次数:12  
标签:默认值 default delphi property open Create 属性

delphi property中default的含义

首先看个案例

TPerson = class
  published
    property Age: Integer read FAge write SetAge default 20;
end;

我们创建一个TPerson类 给其一个属性,然后使用了 default 20 关键字,按照我们的理解 应该是 这个age属性的默认值 就是20;

其实这个default 说明符不是 默认值的意思;官方也特别提示了:

https://docwiki.embarcadero.com/RADStudio/Athens/en/Properties_(Delphi)

image

可见 这个 default 与 我们理解的不一样,为了 降低 熵值,降低复杂度,还是不要使用 这些属性说明符了,就让其默认就可;

若要创建实例时,给其默认值,就在构造函数里 指定;


后来我开发一个按钮组件,给一个按钮默认 一个颜色,发现真的不生效:

//定义属性时,给其指定一个默认值,鼠标悬浮颜色
property ColorHot: TColor read FColorHot write SetColorHot default clWhite;
//在构造函数里指定初始化值,鼠标按下颜色
constructor TColorButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FColorPressed := clRed;
end;

image

所以 还是不要使用 default了,避免 程序难以理解;


default指令的意思不是设置属性默认值。它的意思是:当你在组件可视化设计时设置的属性值和它指定的值不相同时,才将该值存入dfm文件。比如

property  open: boolean   read   FOpen   write   setOpen   default   true;
//如果你在Object   Inspector中设置open为true时,open不会被保存,否则保存到dfm文件中。
//要设置默认值,应该在构造函数Create中设置,如:
constructor     Ttest.Create(AOwner:   TComponent);
begin
    inherited;
    open   :=   true;
    //....
end; 

可见 default 是容易让开发者产生歧义的,故而尽量不要使用

标签:默认值,default,delphi,property,open,Create,属性
From: https://www.cnblogs.com/del88/p/18235178

相关文章

  • 创建属性property时,不用官方的 default 说明符;
    创建属性property时,不用官方的default说明符;首先看个案例TPerson=classpublishedpropertyAge:IntegerreadFAgewriteSetAgedefault20;end;我们创建一个TPerson类给其一个属性,然后使用了default20关键字,按照我们的理解应该是这个age属性的默认值就......
  • delphi 实现登陆窗体 与 主窗体的过程,启动窗口
    登录窗体:typeTfrmLogin=class(TForm)btn1:TButton;procedurebtn1Click(Sender:TObject);private{Privatedeclarations}public{Publicdeclarations}end;varfrmLogin:TfrmLogin;implementation{$R*.dfm}procedureTfrm......
  • can not find lambda cache for this property [] of entity
    这个错误通常出现在使用Hibernate或者类似ORM框架时,当框架尝试缓存一个实体的属性,但是找不到对应的缓存时。这个错误表明框架无法为实体中名为adnm的属性找到合适的缓存机制。解决这个问题的方法通常包括以下几个步骤:确认该属性是否确实存在于实体中,并且已经正确地注解了。如果......
  • 发布 CapstoneDelphi 项目(反汇编引擎 SDK)
    lsuper发布的,以下为他的发布内容:最近遇到一个需要反编译PE32/32+的需求,搜了下GH发现全能的Capstone,不过上面Delphi的实现都比较古老(如Capstone4Delphi)且对不同平台支持的不好,遂借五一基于官方稳定版4.0.2手搓了一个,顺带练练手交叉编译等;经过陆续完善,补全官方所有的tes......
  • delphi Image32 之 快速入门
     官方快速入门,加上了一些注解[从WORD粘贴后失去了样式]TImage32类是关键。TImage32 对象包含单个图像,所有图像操作都作用于此对象。usesImg32; //引用单元...img:=TImage32.Create; //创建TImage32对象//执行一些其它操作img.Free; //用完了要释放图像存储......
  • css29 CSS Layout - The z-index Property
    https://www.w3schools.com/css/css_z-index.asp CSSLayout-Thez-indexProperty  Thez-indexpropertyspecifiesthestackorderofanelement.Thez-indexPropertyWhenelementsarepositioned,theycanoverlapotherelements.Thez-indexproperty......
  • css28 CSS Layout - The position Property
    https://www.w3schools.com/css/css_positioning.aspCSSLayout-ThepositionPropertyThepositionpropertyspecifiesthetypeofpositioningmethodusedforanelement(static,relative,fixed,absoluteorsticky).ThepositionPropertyThepositionpr......
  • css26 CSS Layout - The display Property
    https://www.w3schools.com/css/css_display_visibility.asp  CSSLayout-ThedisplayPropertyThedisplaypropertyisthemostimportantCSSpropertyforcontrollinglayout.ThedisplayPropertyThedisplaypropertyisusedtospecifyhowanelementi......
  • delphi 图形图像处理 Image32
    delpher 越来越少了,但不能掩盖它的优秀,很外前看到了Image32,但发现用它的人很少,这段时间整理了它的资料,重新组合了一个DEMO,也可以说是个小工具,分享出来。----下面的内容不能直接从WORD中复制过来,只能一点点粘贴,Image32 关于Image32说明文档是这样描述的:  用Delphi......
  • Delphi 2010 新增功能之: IOUtils 单元(1): 初识 TDirectory.GetFiles
    用IOUtils单元下的TDirectory.GetFiles获取文件列表太方便了;下面的例子只是TDirectory.GetFiles的典型应用...unitUnit1;interfaceuses Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms, Dialogs,StdCtrls;type TForm1=......