1.接口继承
<?php
interface testA{
function echostr();
}
interface testB{
function dancing($name);
}
interface testC extends testA,testB{
function singing($nickname);
}
class testD implements testC{
function echostr(){
echo "接口继承,要实现父接口所有相关方法!";
echo "<br />";
}
function dancing($name){
echo $name."正在读书!";
echo "<br />";
}
function singing($nickname){
echo $nickname."正在运动!";
}
}
$demo=new testD();
$demo->echostr();
$demo->dancing("小红");
$demo->singing("小张");
?>
结果:
接口继承,要实现父接口所有相关方法!
小红正在读书!
小张正在运动!
2.trait的简单用法
<?php
trait dog{
public $name = 'dog';
public function run(){
echo "dog run";
}
}
class Animal{
public function eat(){
echo "animal eat";
}
}
class cat extends Animal{
use dog;
public function play(){
echo "cat play";
}
}
$cat = new cat();
$cat->play();
echo "<br/>";
$cat->eat();
echo "<br/>";
$cat->run();
?>
结果:
cat play
animal eat
dog run
总结:还是觉得有点难了,有的地方还是不十分明白。只是做了简单示例。还需要多多巩固练习。