Blogger Information
Blog 53
fans 4
comment 3
visits 41770
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php0507之Trait实例
有点凉了
Original
828 people have browsed it

123.gif


实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/8 0008
 * Time: 上午 11:51
 */

class Computer
{
    private $age;
    private $color;

    public function __construct()
    {

    }

    public function Internet($name)
    {
        echo $name."上网设备<br>";
    }

    /**
     * @return mixed
     */
    public function getAge()
    {
        return $this->age;
    }

    /**
     * @return mixed
     */
    public function getColor()
    {
        return $this->color;
    }

}

运行实例 »

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

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/8 0008
 * Time: 上午 11:54
 */

Trait littleCompuer
{
    private $name;
    private $age;
    private $use;

    public function Sell($name){
        return $this->name = $name;
    }
//    public function Internet($name){
//        echo $name."上网办公设备<br>";
//    }
}

运行实例 »

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

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/8 0008
 * Time: 上午 11:56
 */
header("content-type:text/html;charset=utf-8");
require_once "class/Computer.php";
require_once "Trait/littleCompuer.php";
class ThinkPad extends Computer
{
    use littleCompuer;
    public function __construct()
    {

    }

//    public function Internet($name)
//    {
////        parent::Internet($name); // TODO: Change the autogenerated stub
//        echo $name."用来上网办公<br>";
//    }

    public function getAge()
    {
        return parent::getAge(); // TODO: Change the autogenerated stub
    }

    public function getColor()
    {
        return parent::getColor(); // TODO: Change the autogenerated stub
    }

}

$thinkPag = new ThinkPad();
//echo  $thinkPag->Internet("ThinkPad");//如果 ThinkPad 子类同时extend Computer use Trait  并且 自身重载了Internet方法 那么以自身方法输出结果为准,参数不能缺
//echo  $thinkPag->Internet("小型");//如果ThinkPad子类没有重载Internet方法那么往上一级Train寻找,如果有该方法以Trait为准 进行输出
echo  $thinkPag->Internet("Computer");//如果子类 本身 Trait 均没有此方法,再向上一级寻找 也就是 Computer寻找 以父类输出为准

运行实例 »

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


Correction status:qualified

Teacher's comments:
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