我在类函数中,并且想知道父类类型是否为TVMDNode类型。检查ClassParent是否属于X类型(不是:等于X类型)
“ClassParent is TVMDNode”不起作用。 (我不想要“ClassParent = TVMDNode”)。
我该如何完成这项任务:通过类层次结构进行链检查而不需要请求子类实现自己的逻辑?
type
TOID = string;
TOIDPath = array of TOID;
class function TVMDNode.IsSupported(ANode: TOID): boolean; virtual;
begin
result := true;
end;
class function TVMDNode.Supports(ANodePath: TOIDPath): boolean; // not virtual;
var
n: integer;
begin
// Check if the last segment is supported by our class
n := Length(ANodePath);
if not IsSupported(ANodePath[n-1]) then
begin
result := false; Exit;
end;
SetLength(ANodePath, n-1);
// Recursively check if the previous segments are supported by the parent class, as long as they are of type TVMDNode (and therefore have the Supports() function)
// This logic is implemented in the base class TVMDNode only and shall be applied to every descendant class without requiring override
if ClassParent is TVMDNode then // <-- operator not applicable to this operand type
begin
if not (TVMDNode(ClassParent).Supports(ANodePath)) then
begin
result := false; Exit;
end;
end;
result := true; Exit;
end;
直到德尔福6
类家长是不是东西,随时都在变化。为什么你想要检查这个呢?当你写一个课程时,你知道它是从哪里继承的,对吗? – GolezTrol
我想实现后代的逻辑,所以他们不必一次又一次地实现相同的逻辑。例如:TB从继承自TVMDNode的TA继承。调用TB.Supports()应该通过TB.IsSupported(),TA.IsSupported()和TVMDNode.IsSupported()。 TVMDNode的后代只需要实现IsSupported()。 –
classparent是类从其下降的类。没有理由检查这个,就像我之前说过的。但我认为你正在谈论节点的父节点(在层次结构中),这是完全不同的事情。否则你的问题对我来说毫无意义。 – GolezTrol