枚举继承delphi

枚举继承delphi,delphi,enumeration,Delphi,Enumeration,我希望在另一个中继承枚举: 例如: Type TMyTypeKind = (TTypeKind, enBoolean, enPath); 恐怕这根本不可能。你对此无能为力,对不起 键入时: Type TMyTypeKind = (TTypeKind, enBoolean, enPath); Delphi将看到TTypeKind已经是一个类型,它将给出以下错误: [DCC Error] xxx.pas(41): E2004 Identifier redeclared: 'TTypeKind'

我希望在另一个中继承枚举:

例如:

Type TMyTypeKind = (TTypeKind, enBoolean, enPath);

恐怕这根本不可能。你对此无能为力,对不起

键入时:

Type TMyTypeKind = (TTypeKind, enBoolean, enPath);
Delphi将看到
TTypeKind
已经是一个类型,它将给出以下错误:

[DCC Error] xxx.pas(41): E2004 Identifier redeclared: 'TTypeKind'

这是不可能的,因为枚举的名称应该是唯一的。 不能在其他枚举中使用TTypeKind的值,它会产生冲突

然而,在Delphi2009中有一个称为作用域枚举的特性。 你可以说TMyTypeKind.enBoolean

但这并不能解决继承问题

一种方法是将整数常量分配给枚举值:

Type TMyTypeKind = (enBoolean = High(TTypeKind) + 1, enPath = High(TTypeKind) + 2);
因此,您可以有一个以Low(TTypeKind)开始,以High(TMyTypeKind)结束的索引号


你自己去看看:Ord(Enboolian)

你不能。编译器不知道如何解释这个。从:

枚举类型通过简单地列出表示这些值的标识符来定义一组有序的值。这些价值观没有内在意义


它可以通过使用Include文件的技巧来完成。例如:

阿德公司

canonicalName,
cn,
whenCreated,
description,
displayName,
distinguishedName,
instanceType,
memberOf,
modifyTimeStamp,
name,
objectCategory,
objectClass,
objectGuid,
showInAdvancedViewOnly
AdUserGroupCommonAttributes.inc:

msDSPrincipalName,
objectSid,
sAMAccountName
AdUserAttributers.inc:

accountExpires,
badPasswordTime,
badPwdCount,
c,
comment,
company,
department,
division,
employeeID,
givenName,
homeDirectory,
homeDrive,
lastLogon,
lockoutTime,
logonCount,
pwdLastSet,
sn,
telephoneNumber,
tokenGroups,
userAccountControl,
userPrincipalName
单位属性

   TAdUserGroupCommonAttributes = (
    {$I AdCommonAttribs.inc}, {$I AdUserGroupCommonAttributes.inc}
   );
type
  TAdGroupAttributes = (
    {$I AdCommonAttribs.inc},
    {$I AdUserGroupCommonAttributes.inc},
    {$I AdGroupAttributes.inc}
  );
type
  TAdUserAttributes = (
    {$I AdCommonAttribs.inc},
    {$I AdUserGroupCommonAttributes.inc},
    {$I AdUserAttributes.inc}
  );
单位属性

   TAdUserGroupCommonAttributes = (
    {$I AdCommonAttribs.inc}, {$I AdUserGroupCommonAttributes.inc}
   );
type
  TAdGroupAttributes = (
    {$I AdCommonAttribs.inc},
    {$I AdUserGroupCommonAttributes.inc},
    {$I AdGroupAttributes.inc}
  );
type
  TAdUserAttributes = (
    {$I AdCommonAttribs.inc},
    {$I AdUserGroupCommonAttributes.inc},
    {$I AdUserAttributes.inc}
  );
单位成人属性

   TAdUserGroupCommonAttributes = (
    {$I AdCommonAttribs.inc}, {$I AdUserGroupCommonAttributes.inc}
   );
type
  TAdGroupAttributes = (
    {$I AdCommonAttribs.inc},
    {$I AdUserGroupCommonAttributes.inc},
    {$I AdGroupAttributes.inc}
  );
type
  TAdUserAttributes = (
    {$I AdCommonAttribs.inc},
    {$I AdUserGroupCommonAttributes.inc},
    {$I AdUserAttributes.inc}
  );

正如已经说过的,你不能。 但你可以这样做:

TBaseState = class
  public const
    stNone = 1;
    stSingle = 2;
  end;

  TMyState = class(TBaseState)
  public const
    stNewState = 3;
  end;

  var
    state: TMyState;

  begin
    ShowMessage(IntToStr(s.stNewState));
  end;

这与枚举不同,但有时会有所帮助。

类似的事情可能会以相反的顺序出现。如果知道所有可能的值,请将其定义为基类型并声明其子范围类型。子范围将与基类型和彼此兼容。这可能是一种好处,也可能不是

type
 TEnumAll = (enFirst, enSecond, enThird, enFourth, enFifth);
 TEnumLower = enFirst..enThird;
 TEnumMore = enFirst..enFourth;
procedure TForm1.Test1;
var
  All: TEnumAll;
  Lower: TEnumLower;
begin
  for All := Low(TEnumAll) to High(TEnumAll) do begin
   Lower := All;
  end;
  for Lower := Low(TEnumLower) to High(TEnumLower) do begin
    All := Lower;
  end;
end;

你能以什么方式指出这样做的必要性?枚举只是一系列有序升序常量值。不能从枚举继承。是否有任何编程语言允许您从枚举继承?我不知道。Java和C也不允许这样做。