假设我有一个父类和两个这样的子类
abstract class Vehicle {
public function setManufacturer(string $manufacturer) { ... }
}
class Bicycle extends Vehicle {
public function addSaddle() { ... }
}
class Car extends Vehicle {
public function setSpeedLimit(float $limit) { ... }
}
现在我想通过这样的接口使用这些对象
interface VehicleInterface {
public function create(Vehicle $vehicle);
}
class BicycleService implements VehicleInterface {
public function create(Bicycle $bicycle) {
$bicycle->setManufacturer('Some company'); // Common for all Vehicle objects
$bicycle->addSaddle(); // Common only for Bicycle objects
}
}
看来这是不可能的,因为BicycleService create(...)与接口不兼容。除了删除类型提示之外,还有什么办法可以解决这个问题吗?
紫衣仙女
守候你守候我
30秒到达战场
慕尼黑的夜晚无繁华