如何使用 interface 实现多继承呢?
废话少说,上代码!
// 老师
interface TeacherInterface{
public function eat();
public function teach();
}
// 学生
interface StudyInterface{
public function study();
public function shop();
}
// 工人
interface WorkerInterface{
public function workerstudy();
public function workershop();
}
// 老师
class Teacher implements TeacherInterface{
public function eat(){
echo '我在eat<br>';
}
public function teach(){
echo '我在teach<br>';
}
}
// 学生
class Studys implements StudyInterface{
public function study(){
echo '我在study<br>';
}
public function shop(){
echo '我在shop<br>';
}
}
// 工人
class Workers implements WorkerInterface{
public function workerstudy(){
echo '我在workerstudy<br>';
}
public function workershop(){
echo '我在workershop<br>';
}
}
// 接口实现多继承
class Xiaoming extends Studys implements TeacherInterface,WorkerInterface{
private $teacher;
private $workers;
public function __construct(){
$this->teacher = new Teacher();
$this->workers = new Workers();
}
// 实现 TeacherInterface接口
public function eat(){
return $this->teacher->eat();
}
public function teach(){
return $this->teacher->teach();
}
// 实现 WorkerInterface
public function workerstudy(){
return $this->workers->workerstudy();
}
public function workershop(){
return $this->workers->workershop();
}
}
$xiaoming = new Xiaoming();
$xiaoming->eat();
$xiaoming->teach();
$xiaoming->study();
$xiaoming->shop();
$xiaoming->workerstudy();
$xiaoming->workershop();