批改状态:合格
老师批语:接口本质上就是类的模板
<?php//类只能单继承,接口允许多继承interface iSchool{const SCHOOL = '清华大学';public function printSchool();}interface iDepartment{const DEPARTMENT = '自动化系';public function printDepartment();}interface iClass{const CLASSES = '05-01';public function printClasses();}//类可以继承多个接口class StuInfo implements iSchool,iDepartment,iClass{protected $name = 'angle';protected $age = 32;protected $sex = '女';public function printSchool(){echo '校名:',iSchool::SCHOOL,'<br>';}public function printDepartment(){echo '系中:',iDepartment::DEPARTMENT,'<br>';}public function printClasses(){echo '班名:',iClass::CLASSES,'<br>';}public function editStuInfo($name,$age,$sex){$this ->name = $name;$this ->age = $age;$this ->sex = $sex;}public function printStuInfo(){echo '姓名:',$this ->name,'<br>';echo '年龄:',$this ->age,'<br>';echo '性别:',$this ->sex,'<br>';$this ->printClasses();$this ->printDepartment();$this ->printSchool();echo '<hr>';}}$stu1 = new StuInfo();$stu1 -> printStuInfo();$stu2 = new StuInfo();$stu2 -> editStuInfo('hugn',31,'男');$stu2 -> printStuInfo();?>

<?php//可以使用抽象类部分实现接口interface iPrintInfo{public function editSDCInfo(string $school = '清华大学',string $department = '自动化系',string $classes = '05-01');public function printSDCInfo();}//抽象类实现了,学校名称,系名,班名的更改,并没有实现打印功能abstract class aStuInfo implements iPrintInfo{protected $school = '清华大学';protected $department = '自动化系';protected $classes = '05-01';public function editSDCInfo(string $school = '清华大学', string $department = '自动化系', string $classes = '05-01'){$this -> school = $school;$this -> department = $department;$this -> classes = $classes;}}//在工作类中实现学校名称,系名,班名输出class StuInfo extends aStuInfo{protected $name = 'angle';protected $age = 32;protected $sex = '女';public function editStuInfo($name,$age,$sex){$this ->name = $name;$this ->age = $age;$this ->sex = $sex;}public function printStuInfo(){echo '姓名:',$this ->name,'<br>';echo '年龄:',$this ->age,'<br>';echo '性别:',$this ->sex,'<br>';$this ->printSDCInfo();}//接口中的方法没有在抽象类中实现,在工作类中实现public function printSDCInfo(){echo '班名:',$this ->classes,'<br>';echo '系名:',$this ->department,'<br>';echo '校名:',$this ->school,'<br>';echo '<hr>';}}$stu1 = new StuInfo();$stu1 ->printStuInfo();$stu2 = new StuInfo();$stu2 ->editStuInfo('hugn',31,'男');$stu2 -> editSDCInfo('人民大学','工商管理','08-01');$stu2 ->printStuInfo();?>

<?phpinterface iSchool{public function __construct();}class Qhdx implements iSchool{public $school = null;public $department = null;public $classes = null;public function __construct(){$this -> school = '清华大学';$this -> department = '自动化系';$this -> classes = '05-01';}}class Rmdx implements iSchool{public $school = null;public $department = null;public $classes = null;public function __construct(){$this -> school ='人民大学';$this -> department = '工商管理';$this -> classes = '08-02';}}class StuInfo{protected $name = 'angle';protected $age = 32;protected $sex = '女';public function editStuInfo($name,$age,$sex){$this ->name = $name;$this ->age = $age;$this ->sex = $sex;}public function printStuInfo(){echo '姓名:',$this ->name,'<br>';echo '年龄:',$this ->age,'<br>';echo '性别:',$this ->sex,'<br>';}//学校,系,班信息为一个对象,iSchool是一个接口,被两个类继承public function printSDCInfo(iSchool $sInfo){echo '班名:',$sInfo ->classes,'<br>';echo '系名:',$sInfo ->department,'<br>';echo '校名:',$sInfo ->school,'<br>';echo '<hr>';}}//学校信息通过不同的类实例化而成$sInfo = new Qhdx;$stu1 = new StuInfo;$stu1 ->printStuInfo();//打印学校信息时,传入的是一个对象$stu1 ->printSDCInfo($sInfo);$sInfo = new Rmdx;$stu2 = new StuInfo;$stu2 ->editStuInfo('hugn',31,'男');$stu2 ->printStuInfo();$stu2 ->printSDCInfo($sInfo);?>

<?phptrait tPrintSDC{public function printSDCInfo(){echo '班名:',$this ->classes,'<br>';echo '系名:',$this ->department,'<br>';echo '校名:',$this ->school,'<br>';echo '<hr>';}public function editSDCInfo(string $school = '清华大学', string $department = '自动化系', string $classes = '05-01'){$this -> school = $school;$this -> department = $department;$this -> classes = $classes;}}class StuInfo{use tPrintSDC;protected $school = '清华大学';protected $department = '自动化系';protected $classes = '05-01';protected $name = 'angle';protected $age = 32;protected $sex = '女';public function editStuInfo($name,$age,$sex){$this ->name = $name;$this ->age = $age;$this ->sex = $sex;}public function printStuInfo(){echo '姓名:',$this ->name,'<br>';echo '年龄:',$this ->age,'<br>';echo '性别:',$this ->sex,'<br>';$this ->printSDCInfo();}}$stu1 = new StuInfo;$stu1 ->printStuInfo();$stu2 = new StuInfo();$stu2 ->editStuInfo('hugn',31,'男');$stu2 -> editSDCInfo('人民大学','工商管理','08-01');$stu2 ->printStuInfo();?>

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号