You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
696 B

<?php
//逆变
class Food{}
class AnimalFood extends Food{}
abstract class Animal{
protected string $name;
public function __construct(string $name){
$this->name=$name;
}
public function eat(AnimalFood $food){
echo $this->name."eats".get_class($food);
}
}
class Dog extends Animal{
public function eat(Food $food){
echo $this->name."eats".get_class($food);
}
}
$kitty=(new CatShelter)->adopt("ricky");
$catFood=new AnimalFood();
$kitty->eat($catFood).PHP_EOL;
$doggy=(new DogShelter)->adopt("lucky");
$banana=new Food();
$doggy->eat($banana);
?>