📄 07c06-1.php
字号:
<?php// Define an abstract class for animals.abstract class animal { // Declare variables (properties) public $name; // Make a constructor to be shared that requires a name. public function __construct($namesake) { $this->name = $namesake; } // Declare abstract methods that each animal must implement. abstract public function makeSound(); abstract public function eat($what);}// Define a cat classclass cat extends animal { public function makeSound() { echo "Meow\n"; } public function eat($what) { switch ($what) { case 'kibble': case 'cat food': case 'mouse': case 'bird': echo "The cat says yum!\n"; break; case 'grass': case 'tinsel': case 'hair': echo "The cat purrs contentedly.\n"; break; default: echo "The cat raises its head haughtily and walks off.\n"; } }}// Define a cow classclass cow extends animal { public function makeSound() { echo "Mooooooooo!\n"; } public function eat($what) { switch ($what) { case 'grass': case 'hay': case 'salt': echo "The cow chews its cud.\n"; break; default: echo "The cow walks off ignoring it.\n"; } }}// Declare a cat & a cow.$my_cat = new cat('Mimi-ow');$my_cow = new cow('Gertrude');echo '<pre>';// The cat says: Meow$my_cat->makeSound();// The cow says: Moo$my_cow->makeSound();// What do they each do when they eat grass?$my_cat->eat('grass');$my_cow->eat('grass');echo '</pre>';?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -