不知道这个oop模式被称为什么,但我怎么能在Ada中做同样的模式? 例如下面的代码:如何在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);
我在阿达失败的尝试: 如何妥善解决?
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”声明是为时已晚 生成结果的警告:规范应该
我不确定接口,但我知道动态调度基/父包需要在一个单独的包文件中。 – 2017-03-11 00:05:45