我一直在研究AVL樹單元,用戶可以在其中指定他想要的內部樹。我爲此使用對象。在我的單元中,我定義了名爲Node的父對象,並且指向此對象的指針是PTNode。在這個對象中,我有3個屬性:Balance:integer; Left,Right:節點的子節點的PTNode,以及1個方法:Function Is_Greater(Node1:PTNode):虛擬和抽象的整數。這是留給用戶來定義這個函數(我不知道它會是字符或整數等)。Pascal對象中的抽象方法
我試圖測試這個單元,我遇到了一個問題。我創建了我的對象的子對象節點名爲Object1 = Object(Node)並添加了一個屬性X:integer,我想定義函數Is_Greater。這裏是代碼的聲明,部分:在此之後
單位
Unit Tree;
interface
type PTNode=^Node;
Node=object
Left,Right:PTNode;
Balance:integer;
Function Is_Greater(Node1:PTNode):integer; virtual; abstract;
end;
我定義我在我單位的功能不屬於我的問題有關。
這是我的測試程序:
Program Test;
uses Tree;
Type PTObject=^Object1;
Object1=object(Node)
X:integer;
Function Is_Greater(Node1:PTNode):integer; virtual;
end;
Function Object1.Is_Greater(Node1:PTNode):integer;
begin
if X>Node1^.X then Is_Greater:=1
else if X<Node1^.X then Is_Greater:=-1
else Is_Greater:=0;
end;
,這讓我錯誤說是X不是目標節點的一部分。但是,當我嘗試設置Node1:PTObject時,它給了我錯誤,我的功能不匹配其父。我不知道如何解決這個問題。
[Pascal的摘要函數]的可能重複(http://stackoverflow.com/questions/32416605/abstract-function-in-pascal) – gnat