php interface 实现多继承

如何使用 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();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值