Skip to main content

使用接口与组合模拟多继承

通过组合模拟多重继承

在PHP中不支持多重继承,如果我们向使用多个类的方法而实现代码重用有什么办法么?那就是组合。在一个类中去将另外一个类设置成属性。

下面的例子,模拟了多重继承。

class User {
private $name = "tom";
public function getName(){
​ return $this->name;
}
}

class Teacher{
private $lengthOfService = 5; // 工龄
public function getLengthOfService(){
​ return $this->lengthOfService;
}
}
// 上面的类中的set方法就不写了.
// 如果有个研究生,既是学生也算工龄.
class GraduateStudent extends User {
private $teacher ;
public function __construct(){
​ $this->teacher = new Teacher();
}
public function getLengthOfService(){
​ return $this->teacher->getLengthOfService();
}
}

$graduateStudent = new GraduateStudent();
echo "name is ".$graduateStudent->getName()."<br>";
echo "lengthOfService is ".$graduateStudent->getLengthOfService();

程序运行结果:

name is tom lengthOfService is 5

不完全的多重继承

上面的例子是多重继承么?我们继续运行下面的代码。

class User{
private $name = "tom";
public function getName(){
return $this->name;
}
}
class Teacher{
private $lengthOfService = 5; // 工龄
public function getLengthOfService(){
return $this->lengthOfService;
}
}
class GraduateStudent extends User {
private $teacher ;
public function __construct(){
$this->teacher = new Teacher();
}
public function getLengthOfService(){
return $this->teacher->getLengthOfService();
}
}
class Act{
public static function getUserName(User $_user){
echo $_user->getName();
}
pubslic static function getLengthOfService(Teacher $_teacher){
echo $_teacher->getLengthOfService();
}
}
$graduateStudent = new GraduateStudent();
Act::getUserName($graduateStudent);
Act::getLengthOfService($graduateStudent);

程序运行结果:

tom Catchable fatal error: Argument 1 passed to Act::getLengthOfService() must be an instance of Teacher, instance of GraduateStudent given, called in E:\PHPProjects\NowaMagic\php\4-8-2.php on line 34 and defined in E:\PHPProjects\NowaMagic\php\4-8-2.php on line 28

我们并不能将$graduateStudent代表的实例当作Teacher类型传入。

这样的多重继承是失败的,没有多重身份,怎么能叫多重继承呢?

使用接口实现多重继承

通过把类的特征抽象为接口,并通过实现接口的方式让对象有多重身份,自然就可以完全模拟多重继承了。

interface UserInterface{
//定义User的接口
function getname();
}
interface TeacherInterface{
//teacher相关接口
function getLengthOfService();
}
class User implements UserInterface {
//实现UserInterface接口
private $name = "tom";
public function getName(){
return $this->name;
}
}
class Teacher implements TeacherInterface {
//实现TeacherInterface接口
private $lengthOfService = 5;
// 工龄
public function getLengthOfService(){
return $this->lengthOfService;
}
}
// 继承自User类,同时实现了TeacherInterface接口.
class GraduateStudent extends User implements TeacherInterface {
private $teacher ;
public function __construct(){
$this->teacher = new Teacher();
}
public function getLengthOfService(){
return $this->teacher->getLengthOfService();
}
}
class Act{
//注意这里的类型提示改成了接口类型
public static function getUserName(UserInterface $_user){
echo "Name is " . $_user->getName() ."<br>";
}
//这里的类型提示改成了TeacherInterface类型.
public static function getLengthOfService(TeacherInterface $_teacher){
echo "Age is " .$_teacher->getLengthOfService() ."<br>";
}
}
$graduateStudent = new GraduateStudent();
Act::getUserName($graduateStudent);
Act::getLengthOfService($graduateStudent);
// 结果正如我们所要的,实现了有多重身份的一个对象.

程序运行结果:

Name is tom Age is 5