zoukankan      html  css  js  c++  java
  • php怎么实现多态?

    在PHP5中,变量的类型是不确定的,一个变量可以指向任何类型的数值、字符串、对象、资源等。我们无法说PHP5中多态的是变量。

    我们只能说在PHP5中,多态应用在方法参数的类型提示位置。

    一个类的任何子类对象都可以满足以当前类型作为类型提示的类型要求。

    所有实现这个接口的类,都可以满足以接口类型作为类型提示的方法参数要求。

    简单的说,一个类拥有其父类、和已实现接口的身份。

    通过实现接口实现多态(推荐学习:PHP编程从入门到精通)

    <?phpinterface User{ // User接口    public function  getName();    public function setName($_name);}class NormalUser implements User { // 实现接口的类.    private $name;    public function getName(){        return $this->name;    }    public function setName($_name){        $this->name = $_name;    }}class UserAdmin{ //操作.    public static function  ChangeUserName(User $_user,$_userName){        $_user->setName($_userName);    }}$normalUser = new NormalUser();UserAdmin::ChangeUserName($normalUser,"Tom");//这里传入的是 NormalUser的实例.echo $normalUser->getName();?>

    使用接口与组合模拟多继承

    通过组合模拟多重继承。

    在PHP中不支持多重继承,如果我们向使用多个类的方法而实现代码重用有什么办法么?

    那就是组合。在一个类中去将另外一个类设置成属性。

    下面的例子,模拟了多重继承。

    接口实例

    写一个概念性的例子。 我们设计一个在线销售系统,用户部分设计如下: 将用户分为,NormalUser, VipUser, InnerUser 三种。要求根据用户的不同折扣计算用户购买产品的价格。并要求为以后扩展和维护预留空间。

    <?phpinterface User{    public function getName();    public function setName($_name);    public function getDiscount();}abstract class AbstractUser implements User{    private $name = "";    protected  $discount = 0;    protected  $grade = "";    function __construct($_name) {        $this->setName($_name);    }    function getName() {        return $this->name;    }    function setName($_name) {    $this->name = $_name;    }    function getDiscount() {        return $this->discount;    }    function getGrade() {        return $this->grade;    }}class NormalUser extends AbstractUser{    protected $discount = 1.0;    protected $grade = "Normal";}class VipUser extends AbstractUser{    protected $discount = 0.8;    protected $grade = "VipUser";}class InnerUser extends AbstractUser{    protected $discount = 0.7;    protected $grade = "InnerUser";}interface Product{    function getProductName();    function getProductPrice();}interface Book extends Product{    function getAuthor();}class BookOnline implements Book{    private $productName;    protected $productPrice;    protected $Author;    function __construct($_bookName) {        $this->productName = $_bookName;    }    function getProductName() {        return $this->productName;    }    function getProductPrice() {        $this->productPrice = 100;        return $this->productPrice;    }    public function getAuthor() {        $this->Author = "chenfei";        return $this->Author;    }}class Productsettle{    public static function finalPrice(User $_user, Product $_product, $number) {        $price = $_user->getDiscount() * $_product->getProductPrice() * $number;        return $price;    }}$number = 10;$book = new BookOnline("设计模式");$user = new NormalUser("tom");$price = Productsettle::finalPrice($user, $book, $number);$str = "您好,尊敬的" . $user->getName() . "<br />";$str .= "您的级别是" . $user->getGrade() . "<br />";$str .= "您的折扣是" . $user->getDiscount() . "<br />";$str .= "您的价格是" . $price;echo $str;?>
  • 相关阅读:
    js 能实现监听F5页面刷新子iframe 而父页面不刷新
    Thinkpad X201 Gobi2000 上电信3G网络
    【M30】代理类
    C++数组
    【M27】要求或者禁止对象产生于heap之中
    C++ delete operator做了什么事
    【M33】将非尾端类设计为抽象类
    【M32】在未来时态下发展程序
    【M34】如何在同一个程序中结合C++和C
    【M25】将构造方法和非成员方法虚化
  • 原文地址:https://www.cnblogs.com/68xi/p/11528063.html
Copyright © 2011-2022 走看看