根据Ada中的关键字创建对象并调用其方法

Dav*_*Mar 2 pointers ada

这是一个后续我的问题在这里.

我在程序中得到了一点,我觉得我无法继续使用当前的结构,所以我做了很多重写.该Statement类型不再是抽象的,而各亚型Statement创建自己的实例Statement的变量.我还execute从Statements包中删除了抽象函数,因为编译器不喜欢它(每个子类型仍然有自己的execute方法).该execute函数已更改为过程,因为必须修改传入的Statement类型.我将statementFactory(以前的createStatement)移动到Statement包中.

这是我得到的错误:

statements-compoundstatements.adb:15:29: expected type "CompoundStatement" defined at statements-compoundstatements.ads:11
statements-compoundstatements.adb:15:29: found type "Statement'Class" defined at statements.ads:6
Run Code Online (Sandbox Code Playgroud)

我是Ada的初学者,但我的预感是因为execute程序在CompoundStatements(它是语句的"子类")中,它永远无法看到execute语句的另一个"子类" 的方法.我能想到的唯一解决方案是将execute调用execute过程的所有过程转储到Statement包中,但这似乎是不可取的.但这仍然无法解释为什么stmt.all被用作类型Statement'Class而不是创建的类型statementFactory.

这是新代码:

package Statements is

   type Statement is tagged private;

   type Statement_Access is access all Statement'Class;
   ParserException : Exception;

   procedure createStatement(tokens : Vector; S : out Statement);
   procedure statementFactory(S: in out Statement; stmt: out Statement_Access);

--.....A bunch of other procedures and functions.....

private
   type Statement is tagged
      record
         tokens : Vector;
         executedtokens : Vector;
      end record;

end Statements;
Run Code Online (Sandbox Code Playgroud)
   procedure createStatement(tokens : Vector; S : out Statement) is
   begin
      S.tokens := tokens;
   end createStatement;

   procedure statementFactory(S: in out Statement; stmt: out Statement_Access) is
      currenttoken : Unbounded_String;
      C            : CompoundStatement;
      A            : AssignmentStatement;
      P            : PrintStatement;

   begin
      currenttoken := getCurrentToken(S);
      if currenttoken = "begin" then
         createStatement(S.tokens, C);
         stmt := new CompoundStatement;
         stmt.all := Statement'Class(C);
      elsif isVariable(To_String(currenttoken)) then
         createStatement(S.tokens, A);
         stmt := new AssignmentStatement;
         stmt.all := Statement'Class(A);
      elsif currenttoken = "print" then
         createStatement(S.tokens, P);
         stmt := new PrintStatement;
         stmt.all := Statement'Class(P);
   end statementFactory;
Run Code Online (Sandbox Code Playgroud)
package body Statements.CompoundStatements is

   procedure execute(skip: in Boolean; C: in out CompoundStatement; reset: out Integer) is
      stmt: Statement_Access;
      tokensexecuted: Integer;
      currenttoken : Unbounded_String;
   begin
      match(C, "begin");
      currenttoken := getCurrentToken(C);
      while(currenttoken /= "end") loop
         statementFactory(C, stmt);
         execute(skip, stmt.all, tokensexecuted); //ERROR OCCURS HERE
Run Code Online (Sandbox Code Playgroud)

Sim*_*ght 5

你说"我还从Statements包中删除了抽象执行函数,因为编译器不喜欢它"; 但你真的需要它,因为否则编译器如何知道当你调用execute (skip, stmt.all, tokensexecuted)any stmt.all会提供一个execute派遣它?

扩展类型不仅继承了其父类型的属性(因此每个CompoundStatement等等都已经拥有tokensexecutedtokens),它继承了父类的基本操作; 如果父操作是abstract,则子项必须提供自己的实现,否则,子项可以提供自己的(重写)实现.

有关此问题的详细讨论,请参阅Ada 95 RationaleWikibooks.