首页 > 其他分享 >创建属性property时,不用官方的 default 说明符;

创建属性property时,不用官方的 default 说明符;

时间:2024-06-06 12:32:57浏览次数:10  
标签:默认值 20 default 说明符 property 属性

创建属性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了,避免 程序难以理解;

标签:默认值,20,default,说明符,property,属性
From: https://www.cnblogs.com/del88/p/18234915

相关文章

  • can not find lambda cache for this property [] of entity
    这个错误通常出现在使用Hibernate或者类似ORM框架时,当框架尝试缓存一个实体的属性,但是找不到对应的缓存时。这个错误表明框架无法为实体中名为adnm的属性找到合适的缓存机制。解决这个问题的方法通常包括以下几个步骤:确认该属性是否确实存在于实体中,并且已经正确地注解了。如果......
  • 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......
  • nginx 默认虚拟主机,default_server
    nginx虚拟主机是通过HTTP请求中的Host值来找到对应的虚拟主机配置,如果找不到呢?那Nginx就会将请求送到指定了default_server的节点来处理,如果没有指定为default_server的话,找conf.d目录下字母排序位于第一个的配置文件中的第一个server_name节点了。nginx案例,理解defau......
  • Qt 动画类(QPropertyAnimation)
    前言QPropertyAnimation是QT中的一个动画类,用于对目标对象的属性进行动画效果展示。该类继承自QAbstractAnimation类,使用起来非常方便和灵活。一、QPropertyAnimation类介绍QPropertyAnimation可以对任何QObject的子类的属性进行动画的展示,只要该属性是可写的,即存在set方......
  • DefaultListableBeanFactory+ GenericBeanDefinition
    定义与用途:GenericBeanDefinition:它是Spring框架中用于定义通用Bean的一个类。它继承自抽象类AbstractBeanDefinition,并增加了一个成员属性parentName。这个类主要用于存储Bean的配置信息,包括Bean的类名、作用域、属性等。DefaultListableBeanFactory:它是SpringIoC容器的一个......
  • 自定义CSS属性(@property)解决自定义CSS变量无法实现过渡效果的问题
    且看下面的代码:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><metaname="viewport"content="width=device-width,initial-scale=1.0"/><title>demot</t......
  • 接口报错.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework
    1、报文:.w.s.m.s.DefaultHandlerExceptionResolver:Resolved[org.springframework.http.converter.HttpMessageNotReadableException:JSONparseerror:Unexpectedcharacter('''(code39)):wasexpectingdouble-quotetostartfieldname;nestedex......
  • Spring 框架类PropertySourcesPlaceholderConfigurer
    PropertyOverrideConfigurer是Spring框架中的一个类,它允许你在Spring的配置文件之外通过外部属性文件来覆盖已定义的bean属性。这在部署不同的环境(如开发、测试、生产)时特别有用,因为你可以为不同的环境定义不同的属性,而无需修改Spring的配置文件。演示:创建实体类:p......