Non so come si chiami questo modello oop ma come posso fare lo stesso modello in Ada? Per esempio questo codice:Come implementare l'interfaccia in 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);
mio tentativo fallito in Ada: Come risolvere in modo corretto?
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;
compilatore dice: risultati avvertimento Costruttore: dichiarazione di "TestVehicle" è troppo tardi risultati Builder avvertimento: spec dovrebbe apparire immediatamente dopo la dichiarazione di "veicolo"
Non sono sicuro delle interfacce, ma so che con l'invio dinamico il pacchetto base/genitore deve trovarsi in un file di pacchetto separato. –