小编Mtw*_*ark的帖子

从Delphi中的类引用变量访问类常量

我正在使用Delphi 2007来维护一个旧项目,我在从Class Reference变量访问类常量时遇到问题,我总是得到父类常量而不是子类常量.

假设有一个父类,一些子类,一个类引用,最后是一个const数组来存储类引用以进行循环.

看看以下简单的程序:

program TestClassConst;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type

  TParent = class
  const
    ClassConst = 'BASE CLASS';
  end;

  TChild1 = class(TParent)
  const
    ClassConst = 'CHILD 1';
  end;

  TChild2 = class(TParent)
  const
    ClassConst = 'CHILD 2';
  end;

  TParentClass = class of TParent;
  TChildClasses = array[0..1] of TParentClass;

const
  ChildClasses: TChildClasses = (TChild1, TChild2);

var
  i: integer;
  c: TParentClass;
  s: string;

begin
  try
    writeln;

    writeln('looping through class reference array');
    for i := low(ChildClasses) to high(ChildClasses) do begin
      c := …
Run Code Online (Sandbox Code Playgroud)

delphi inheritance const class-constants

8
推荐指数
1
解决办法
1221
查看次数

标签 统计

class-constants ×1

const ×1

delphi ×1

inheritance ×1