私有类型对所有人可见?
Private types are visible to everyone?
在程序开发过程中,我无意中注意到在 classes 中声明的所有类型都具有全局可见性。
我一直认为它们的可见性仅限于 class 除非使用 class 类型名称引用类型,例如 TMyClass.TMytype.Value;
我是不是真的做错了什么,结构如下:
unit MyTest;
interface
type TMyTest = class
constructor Create;
strict private
type TMyType = ( NUL, SLEEP );
end;
implementation
// ...
导致使用此 (MyTest) 单元的其他单元发生冲突。
如果设备有睡眠( 100 );调用,它将与 TMyTest.TMyType.SLEEP 发生冲突,为了防止冲突,我首先将 SLEEP 封装在 class 和 TMyType 中。
有任何解决方法的建议吗?
这实际上是设计使然。您的枚举值具有单位或全局范围。它们不是私有的,因为它们不属于 class。它们的范围在全球范围内。
您可以通过包含 scoped enums 指令来安排枚举值具有局部范围:
{$SCOPEDENUMS ON}
The $SCOPEDENUMS
directive enables or disables the use of scoped enumerations in Delphi code. More specifically, $SCOPEDENUMS
affects only definitions of new enumerations, and only controls the addition of the enumeration's value symbols to the global scope.
In the {$SCOPEDENUMS ON}
state, enumerations are scoped, and enum values are not added to the global scope. To specify a member of a scoped enum, you must include the type of the enum.
在程序开发过程中,我无意中注意到在 classes 中声明的所有类型都具有全局可见性。
我一直认为它们的可见性仅限于 class 除非使用 class 类型名称引用类型,例如 TMyClass.TMytype.Value;
我是不是真的做错了什么,结构如下:
unit MyTest;
interface
type TMyTest = class
constructor Create;
strict private
type TMyType = ( NUL, SLEEP );
end;
implementation
// ...
导致使用此 (MyTest) 单元的其他单元发生冲突。
如果设备有睡眠( 100 );调用,它将与 TMyTest.TMyType.SLEEP 发生冲突,为了防止冲突,我首先将 SLEEP 封装在 class 和 TMyType 中。
有任何解决方法的建议吗?
这实际上是设计使然。您的枚举值具有单位或全局范围。它们不是私有的,因为它们不属于 class。它们的范围在全球范围内。
您可以通过包含 scoped enums 指令来安排枚举值具有局部范围:
{$SCOPEDENUMS ON}
The
$SCOPEDENUMS
directive enables or disables the use of scoped enumerations in Delphi code. More specifically,$SCOPEDENUMS
affects only definitions of new enumerations, and only controls the addition of the enumeration's value symbols to the global scope.In the
{$SCOPEDENUMS ON}
state, enumerations are scoped, and enum values are not added to the global scope. To specify a member of a scoped enum, you must include the type of the enum.