PHP实现多重继承--接口方式实现多重继承(完全模拟)
2016-11-25 01:02:50 0 举报
PHP实现多重继承--接口方式实现多重继承(完全模拟)
作者其他创作
大纲/内容
private $name = 'lisi'; function getName() { // TODO: Implement getName() method. echo 'Student name is ' . $this-name . ''; }
GraduateTeacher
- work_year=2
+ getName()
$lisi = new GraduateStudent();$lisi-getName();$lisi-getWorkYear();echo '';Test::getName($lisi);Test::getWorkYear($lisi); //这里不会报错,因为$lisi所属的类GraduateStudent,实现了Teacher的接口,相当于Teacher身份
User
GraduateStudent
- teacher:GraduateTeacher
+ __construct()+ getWorkYear()
private $teacher; public function __construct() { $this-teacher = new GraduateTeacher(); } function getWorkYear() { $this-teacher-getWorkYear(); }
Test
+ getName(User $student)+ getWorkYear(Teacher $teacher)
static function getName(User $student) { $student-getName(); } //当Teacher改成GraduateTeacher,到后面代码行的“Test::getWorkYear($lisi);”就会报错 static function getWorkYear(Teacher $teacher) { $teacher-getWorkYear(); }
Teacher
+ getWorkYear()
Student
- name='lisi'
private $work_year = 2; function getWorkYear() { // TODO: Implement getWorkYear() method. echo 'Teacher work year is ' . $this-work_year . ''; }
0 条评论
下一页
为你推荐
查看更多