作用:将两个不相关的类结合在一起,通过两个类中的方法和属性,得到新的功能
// 讲师分配
abstract class Students
{
abstract public function classroom();
}
class OpenStudents extends Students
{
public function classroom()
{
return '公开课教室1,公开课教室2,公开课教室3';
}
}
class VipStudents extends Students
{
public function classroom()
{
return 'VIP教室1,VIP教室2,VIP教室3';
}
}
// 学员上课
abstract class Listen
{
abstract public function project($who, $course);
}
class Course extends Listen
{
public function project($who, $course)
{
return $course . '这几个教室正在上' . $who;
}
}
class StudentListen
{
public $classroom;
public $course;
public function __construct($classroom, $course)
{
$this->classroom = $classroom;
$this->course = $course;
}
public function privilege($who)
{
$roomName = $this->classroom->classroom();
return $this->course->project($roomName, $who);
}
}
$classroom = new OpenStudents(); // 公开课教室
$course = new Course();
$obj = new StudentListen($classroom, $course);
echo $obj->privilege('swoole');