Không biết mô hình oop này được gọi là gì, nhưng làm thế nào tôi có thể làm cùng một mẫu trong Ada? Ví dụ mã này:Làm cách nào để triển khai giao diện trong 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);
thất bại của tôi trong Ada: Làm thế nào để sửa chữa nó đúng cách?
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;
Compiler nói: Builder kết quả cảnh báo: tuyên bố "TestVehicle" là quá muộn kết quả Builder cảnh báo: spec sẽ xuất hiện ngay sau khi tuyên bố "xe"
Tôi không chắc chắn về giao diện, nhưng tôi biết với điều động động, gói cơ sở/gói gốc cần phải nằm trong tệp gói riêng biệt. –