PHP 接口的使用场景,trait的使用
程序员文章站
2022-05-16 21:41:27
...
学习总结
<?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();
?>
- 代码运行结果
2.可以用抽象类部分实现接口中的功能,在工作类中再实现接口中的剩余功能
<?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();
?>
- 代码运行结果
3.接口中的构造函数被多个类继承,工作类中可以通过接口访问到每个类中的数据
<?php
interface 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);
?>
- 代码运行结果
4.trait的使用,trait实现类中重复代码的复用
<?php
trait 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();
?>
- 代码运行结果