php面向接口编程

来源:互联网 发布:魔兽数据库7.0手机 编辑:程序博客网 时间:2024/06/09 14:33
<?php//定义接口interface User{    function getDiscount();    function getUserType();}//VIP用户 接口实现class VipUser implements User{    // VIP 用户折扣系数    private $discount = 0.8;    function getDiscount() {        return $this->discount;    }    function getUserType() {        return "VIP用户";    }}class Goods{    var $price = 100;    var $vc;    //定义 User 接口类型参数,这时并不知道是什么用户    function run(User $vc){        $this->vc = $vc;        $discount = $this->vc->getDiscount();$usertype = $this->vc->getUserType();        echo $usertype."商品价格:".$this->price*$discount;    }}$display = new Goods();$display ->run(new VipUser);//可以是更多其他用户类型?>
0 0
原创粉丝点击