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.

31 lines
696 B

3 years ago
  1. <?php
  2. //逆变
  3. class Food{}
  4. class AnimalFood extends Food{}
  5. abstract class Animal{
  6. protected string $name;
  7. public function __construct(string $name){
  8. $this->name=$name;
  9. }
  10. public function eat(AnimalFood $food){
  11. echo $this->name."eats".get_class($food);
  12. }
  13. }
  14. class Dog extends Animal{
  15. public function eat(Food $food){
  16. echo $this->name."eats".get_class($food);
  17. }
  18. }
  19. $kitty=(new CatShelter)->adopt("ricky");
  20. $catFood=new AnimalFood();
  21. $kitty->eat($catFood).PHP_EOL;
  22. $doggy=(new DogShelter)->adopt("lucky");
  23. $banana=new Food();
  24. $doggy->eat($banana);
  25. ?>