Blogger Information
Blog 42
fans 0
comment 1
visits 25879
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
trait实例-2018年5月8日19:15完成
邵军-山东-84918的博客
Original
475 people have browsed it

实例

<?php
class Hobby
{
    protected $name;
    public function __construct($name='王五')
    {
        $this->name=$name;
    }
    public function activity($subject){
        return $this->name.'喜欢'.$subject.'游戏。';
    }
}
trait Other1
{
    public $classmate='张三';
    public function sport($name='同学')
    {
        return $this->name.'和'.$this->classmate.'是'.$name;
    }

    public function activity($subject){
        return $this->name.'喜欢'.$subject.'运动项目。';
    }
}
trait Other2
{
    public $classmate1='李四';
    public function sport($name='校友')
    {
        return $this->name.'和'.$this->classmate1.'是'.$name;
    }
}
class Enjoy extends Hobby
{
    use Other1, Other2 {
        Other1::sport insteadof Other2;
        Other2::sport as mysport;
    }

    public function activity($subject = '乒乓球')
    {
        return $this->name . '喜欢' . $subject . '运动项目。';

    }
    public function activity1()
    {
        return parent::activity($subject='射击');
    }
}
$enjoy=new Enjoy();
//调用子类,级别高于父类
echo $enjoy->activity();
echo '<hr>';
//调用父类
echo $enjoy->activity1();
echo '<hr>';
//调用Other1
echo $enjoy->sport();
echo '<hr>';
//调用Other2
echo $enjoy->mysport();
echo '<hr>';

运行实例 »

点击 "运行实例" 按钮查看在线实例


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post