Pascal中的抽象函数 [英] Abstract function in Pascal

查看:110
本文介绍了Pascal中的抽象函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究AVL-tree单元,用户可以在其中指定他想要在树中包含的内容.我为此目的使用对象.在我的单元中,我定义了一个称为Node的父对象,指向该对象的指针是PTNode.在这个对象中,我有3个属性,分别是Balanced:integer; Left,Right:PTNode(代表该节点的子节点),以及1个方法:Function Is_Greater(Node1:PTNode):integer,它是虚拟的和抽象的.并由用户来定义此函数(我不知道它将是char还是integer等).

I have been working on AVL-tree unit where user can specify what he wants to have inside of the tree. I'm using objects for this purpose. In my unit I defined parent Object called Node and pointer to this object is PTNode. In this object I have 3 attributes which are Balance:integer;Left,Right:PTNode for sons of the node, and 1 method:Function Is_Greater(Node1:PTNode):integer which is virtual and abstract. And it is left up to user to define this function(I don't know whether it will be char or integer etc).

我正在尝试测试本机,但遇到了一个问题.我创建了名为Object1 = Object(Node)的对象Node的子对象,并添加了一个属性X:integer,并且我想定义功能Is_Greater.这是声明和代码的一部分:

I was trying to test this unit and I came across one problem. I created child object of my object Node called Object1=Object(Node) and added one attribute X:integer and I want to define the Function Is_Greater. Here is the declaration and part of code:

单位

Unit Tree;
  interface
    type PTNode=^Node;  
         Node=object
            Left,Right:PTNode;    
            Balance:integer;
            Function Is_Greater(Node1:PTNode):integer; virtual; abstract;
          end;

此后,我列出并实现了与我的问题无关的功能.

after this I list and implement functions in my unit which are not that relevant to my problem.

这是我的测试程序:

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时,它给了我错误,提示我的函数与其父函数不匹配.我不知道该怎么解决.

and it gives me error saying that X is not part of Object Node. But when I try to set Node1:PTObject then it gives me error that my function doesn't match its parent. I don't know how to solve this.

推荐答案

您需要输入类型转换参数Node1:

You need to type cast the argument Node1:

if X>PTObject(Node1)^.X then Is_Greater:=1
else if X<PTObject(Node1)^.X then Is_Greater:=-1
else Is_Greater:=0;

这篇关于Pascal中的抽象函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆