gpt4 book ai didi

oop - 如何在 Ada 中实现接口(interface)?

转载 作者:行者123 更新时间:2023-12-04 17:10:36 24 4
gpt4 key购买 nike

不知道这个 oop 模式叫什么,但我怎样才能在 Ada 中做同样的模式?
例如这段代码:

interface Vehicle{
string function start();
}

class Tractor implements Vehicle{
string function start(){
return "Tractor starting";
}
}
class Car implements Vehicle{
string function start(){
return "Car starting";
}
}

class TestVehicle{
function TestVehicle(Vehicle vehicle){
print( vehicle.start() );
}
}
new TestVehicle(new Tractor);
new TestVehicle(new Car);

我在 Ada 中的失败尝试:
如何正确修复?
with Ada.Text_IO;

procedure Main is

package packageVehicle is
type Vehicle is interface;
function Start(Self : Vehicle) return String is abstract;
end packageVehicle;

type Tractor is new packageVehicle.Vehicle with null record;
overriding -- optional
function Start(Self : Tractor) return string is
begin
return "Tractor starting!";
end Start;
type Car is new packageVehicle.Vehicle with null record;
overriding -- optional
function Start(Self : Car) return string is
begin
return "Car starting!";
end Start;


procedure TestVehicle(Vehicle : packageVehicle.Vehicle) is
begin
Ada.Text_IO.Put_Line( "Testing a vehicle" );
Ada.Text_IO.Put_Line( Start(Vehicle) );
end;

Tractor0 : Tractor;
Car0 : Car;

begin

Ada.Text_IO.Put_Line( TestVehicle(Tractor0) );
Ada.Text_IO.Put_Line( TestVehicle(Car0) );

end Main;

编译器说:
生成器结果警告:“TestVehicle”的声明为时已晚
生成器结果警告:规范应在“车辆”声明后立即出现

最佳答案

需要注意的关键是“接口(interface)类型的所有用户定义的原始子程序都应该是抽象子程序或空过程”。 ( Ref ) 即您不能定义将接口(interface)本身作为参数的子程序(是的,我知道这与 Java 不同。)这就是您在 TestVehicles 声明中收到错误的原因。

本质上,您必须定义一个实现接口(interface)的类型,然后使用该类型。

关于 Interfaces 的 Ada 基本原理一章对此进行了一些详细的讨论。

这是一个基于您的问题的工作示例-我重命名了一些内容并修复了一些错误,这些错误可能在您看到的错误消息中丢失了:-) 请注意添加了一个实例化 Vehicle 接口(interface)的类型“Concrete_Vehicles”。

with Ada.Text_IO; use Ada.Text_IO;

procedure Interface_Test is

package Package_Vehicle is
type Vehicle is interface;

function Start(Self : Vehicle) return String is abstract;
end Package_Vehicle;


type Concrete_Vehicles is abstract new Package_Vehicle.Vehicle with null record;


type Tractor is new Concrete_Vehicles with null record;

overriding -- optional
function Start(Self : Tractor) return string is
begin
return "Tractor starting!";
end Start;

type Car is new Concrete_Vehicles with null record;
overriding -- optional
function Start(Self : Car) return string is
begin
return "Car starting!";
end Start;


procedure TestVehicle(Vehicle : Concrete_Vehicles'Class) is
begin
Ada.Text_IO.Put_Line( "Testing a vehicle" );
Ada.Text_IO.Put_Line( Start(Vehicle) );
end;

Tractor0 : Tractor;
Car0 : Car;

begin

TestVehicle(Tractor0);
TestVehicle(Car0);

end Interface_Test;

编译和运行:
[22] Marc say: gnatmake interface_test.adb
gcc -c interface_test.adb
gnatbind -x interface_test.ali
gnatlink interface_test.ali
[23] Marc say: ./interface_test
Testing a vehicle
Tractor starting!
Testing a vehicle
Car starting!

关于oop - 如何在 Ada 中实现接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16376999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com