在Delphi中创建和使用静态类的静态属性,就像.NET一样
我是一名.NET开发人员,非常熟悉Delphi 我想创建一个静态类并使用如下属性在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;
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上的类 最后,我通过将这些属性添加到我的单元来解决我的问题,如下所示:
我如何实现它?我应该创建一个单元吗?我有很多错误,你不必创建一个新的单元,你可以在一个单元中声明多个类。只需复制除type关键字之外的代码,并将其粘贴到existingtype关键字下的单元中。但是要有一个干净的代码,将对象拆分成不同的单元是一个很好的做法。我想为此设置静态数据,并在其他类上使用它。如何将[class属性Red:Integer read FRed write FRed;]更改为class属性myprop:Integer read'10'write'10';您希望道具为只读吗?@sddk您可以在一个单元中拥有多个
类型关键字!不,这不是一个静态属性!你们定义的是一个全局常数,我所需要的就是这个属性,我可以在整个解决方案的任何地方调用它