<?php
//tarait 代码复用
//tarait 工作在继承的上下文环境中,它是位于父类与子类之间的
//tarait 的优先级是高于父类 低于子类
//tarait 命名冲突的解决方法 替换或 别名
trait Func1
{
//驾驶
public function drive()
{
return '支持无人驾驶';
}
public function ct()
{
return '命名1';
}
}
trait Func2
{
public function ct()
{
return '命名2';
}
}
class Auto
{
public $brand;//品牌
public $purpose;//用途
//构造器
public function __construct($brand,$purpose)
{
$this->brand = $brand;
$this->purpose=$purpose;
}
//保养
public function care()
{
return '保养汽车';
}
}
//定义一个子类
class Bus extends Auto
{
//子类Bus 除了可以继承Bus中的成员 还可以导入 trait类中的方法
// use Func1;
use Func1,Func2
{
Func1::ct insteadof Func2;
Func2::ct as ctt;
}
//定义一个care方法 将覆盖原来父类的方法
public function care()
{
return '保养维修汽车';
}
}
//实例化子类
$bus = new Bus('比亚迪','公交车');
echo $bus->brand,'</br>';
echo $bus->purpose,'</br>';
echo $bus->drive(),'</br>';
echo $bus->ctt();
4.10Trait特性技术详解
最新推荐文章于 2023-02-07 14:16:35 发布
