Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Delphi中创建和使用静态类的静态属性,就像.NET一样_Delphi_Static Methods - Fatal编程技术网

在Delphi中创建和使用静态类的静态属性,就像.NET一样

在Delphi中创建和使用静态类的静态属性,就像.NET一样,delphi,static-methods,Delphi,Static Methods,我是一名.NET开发人员,非常熟悉Delphi 我想创建一个静态类并使用如下属性 MyVariable := MyStaticClass.MyProperty; 我不知道怎么做。为此使用类属性 type TMyClass = class strict private class var // Note fields must be declared as class fields FRed: Integer;

我是一名.NET开发人员,非常熟悉Delphi

我想创建一个静态类并使用如下属性

MyVariable := MyStaticClass.MyProperty;

我不知道怎么做。

为此使用
类属性

 type
    TMyClass = class
      strict private
        class var         // Note fields must be declared as class fields
           FRed: Integer;
           FGreen: Integer;
           FBlue: Integer;
        public             // ends the class var block
           class property Red: Integer read FRed write FRed;
           class property Green: Integer read FGreen write FGreen;
           class property Blue: Integer read FBlue write FBlue;
    end
在此示例中,名为
红色
绿色
蓝色
的属性可以在没有对象引用的情况下使用。用于文件

声明只读类(静态)属性

  TMyClass = class
    private
      const FRedValue:Integer = 10;
    public
      class property Red: Integer read FRedValue;
  end;
用这个,

aColorVariable := TMyClass.Red;

提示:单元somhow等于.NET上的类 最后,我通过将这些属性添加到我的单元来解决我的问题,如下所示:

  • 通过右键单击解决方案名称并添加>单元来创建单元

  • 声明一个常量,如下所示:

    常量myPropertyName:Integer=0

  • 将它添加到您的表单中,您希望由谁使用它

    使用 你的单位名称

  • 之后

    接口

    表格上的一节

  • 我需要做的就是键入我在单元上声明的属性名(myPropertyName)

  • 我如何实现它?我应该创建一个单元吗?我有很多错误,你不必创建一个新的单元,你可以在一个单元中声明多个类。只需复制除type关键字之外的代码,并将其粘贴到existingtype关键字下的单元中。但是要有一个干净的代码,将对象拆分成不同的单元是一个很好的做法。我想为此设置静态数据,并在其他类上使用它。如何将[class属性Red:Integer read FRed write FRed;]更改为class属性myprop:Integer read'10'write'10';您希望道具为只读吗?@sddk您可以在一个单元中拥有多个
    类型
    关键字!不,这不是一个静态属性!你们定义的是一个全局常数,我所需要的就是这个属性,我可以在整个解决方案的任何地方调用它