【发布时间】:2013-05-04 17:23:38
【问题描述】:
不知道这个 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”的声明为时已晚 生成器结果警告:规范应在“车辆”声明后立即出现
【问题讨论】:
-
我不确定接口,但我知道动态调度基础/父包需要在单独的包文件中。