界面是一个 abstract tagged null record 在Ada 95:
abstract tagged null record
package Abstract_Driver is type Instance is abstract tagged null record; subtype Class is Instance'Class; --' (defect syntax highlighter) function Image (Item : in Instance) return String is abstract; procedure Read_Line (Item : in out Instance; Buffer : out String) is abstract; end Abstract_Driver;
with Abstract_Driver; package Text_IO_Driver is subtype Parent is Abstract_Driver.Instance; type Instance is new Parent with private; subtype Class is Instance'Class; --' (defect syntax highlighter) function Image (Item : in Instance) return String; Buffer_Too_Small : exception; procedure Read_Line (Item : in out Instance; Buffer : out String); private type Instance is new Parent with null record; end Text_IO_Driver;
with Ada.Text_IO; package body Text_IO_Driver is function Image (Item : in Instance) return String is begin return "Ada.Text_IO.Standard_Input"; end Image; procedure Read_Line (Item : in out Instance; Buffer : out String) is Last : Natural; begin Buffer := (Buffer'Range => ' '); --' (defect syntax highlighter) Ada.Text_IO.Get_Line (Item => Buffer, Last => Last); if Last = Buffer'Last then --' (defect syntax highlighter) raise Buffer_Too_Small; end if; end Read_Line; end Text_IO_Driver;
我所知道的唯一一个如下所述,可能不是 典范 。这不是严格的接口继承,但它可以让你朝着正确的方向。 它需要使用判别式标记记录。
诀窍是定义2个标记类型。一个是经典类定义,另一个是“接口”继承。
然后,您可以使用判别式操作一个对象,该对象可以访问接口契约和类契约。在同一个包中声明两者应该可以让您完全了解私有部分,并进行确认。
简而言之 :
type InterfaceX is abstract ....; -- abstract class and services type ClassA is tagged ...; -- or is new .... type Trick (component : ClassA) is new InterfaceX ...; -- this type gives you access to classA and interfaceX primitives
Trick对象实现了您的InterfaceX契约。
您必须为ClassA对象或Trick对象定义instantiaton / accessors。我认为类型也应该是有限的。
我总是听到人们称之为“罗森诡计”,猜测它是以J.-P.命名的。罗森。
也许你会在这里找到一些更精确的答案 http://www.adaic.org/resources/add_content/standards/95rat/rat95html/rat95-p2-4.html#6