name = $name; } public function getName(): string { return $this->name; } // Phương thức trừu tượng bắt buộc lớp con phải định nghĩa lại abstract public function makeSound(): string; } interface CanRun { public function run(): string; } class Dog extends Animal implements CanRun { public function makeSound(): string { return "Gâu gâu! (Woof! Woof!)"; } public function run(): string { return "Chú chó {$this->name} đang chạy rất nhanh bằng 4 chân trên sân cỏ!"; } } class Cat extends Animal { public function makeSound(): string { return "Meo meo! (Meow! Meow!)"; } } $dogName = trim($_POST['dog_name'] ?? 'Milu'); $catName = trim($_POST['cat_name'] ?? 'Mimi'); $dog = new Dog($dogName ?: 'Milu'); $cat = new Cat($catName ?: 'Mimi'); $animals = [$dog, $cat]; ?> Bài 04 - Abstract Class & Interface | Lab 02 OOP

Bài 04: Abstract Class Animal & Interface CanRun

Lớp trừu tượng Animal (makeSound()), lớp Dog, Cat và Interface CanRun (run())

Đặt tên đối tượng thú cưng
Class extends Animal
getName()) ?>

makeSound():
makeSound()) ?>


Interface CanRun:
implements CanRun
run()) ?> Không thực thi CanRun