我想创建一个具有'多态'配置的记录(结构)。它会在所有情况下使用几个字段,并且我只在需要时才使用其他字段。我知道我可以通过在记录中声明的变体部分来完成此操作。我不知道在设计时是否有可能只能访问我需要的元素。更具体的看下面的例子:德尔福 - 不同部分的记录
program consapp;
{$APPTYPE CONSOLE}
uses
ExceptionLog,
SysUtils;
type
a = record
b : integer;
case isEnabled : boolean of
true : (c:Integer);
false : (d:String[50]);
end;
var test:a;
begin
test.b:=1;
test.isEnabled := False;
test.c := 3; //because isenabled is false, I want that the c element to be unavailable to the coder, and to access only the d element.
Writeln(test.c);
readln;
end.
这可能吗?
+1。我80%确定这是答案... – RBA
这是完全正确的,但在这种情况下,我宁愿使用类而不是记录。它将允许添加继承功能,这在此处是有意义的(例如,IsEnable属性通常在父级别处理,并在儿童之间共享)。 –