在PHP5中實現(xiàn)多態(tài)的方法有哪些-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關在PHP5中實現(xiàn)多態(tài)的方法有哪些,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

目前成都創(chuàng)新互聯(lián)已為上千多家的企業(yè)提供了網(wǎng)站建設、域名、虛擬主機、網(wǎng)站托管、服務器租用、企業(yè)網(wǎng)站設計、雞東網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

通過實現(xiàn)接口實現(xiàn)多態(tài)


復制代碼 代碼如下:


<?php
interface User{ // User接口
    public function  getName();
    public function setName($_name);
}

class NormalUser implements User { // 實現(xiàn)接口的類.
    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中不支持多重繼承,如果我們向使用多個類的方法而實現(xiàn)代碼重用有什么辦法么?

那就是組合。在一個類中去將另外一個類設置成屬性。

下面的例子,模擬了多重繼承。

接口實例

寫一個概念性的例子。 我們設計一個在線銷售系統(tǒng),用戶部分設計如下: 將用戶分為,NormalUser, VipUser, InnerUser 三種。要求根據(jù)用戶的不同折扣計算用戶購買產(chǎn)品的價格。并要求為以后擴展和維護預留空間。


復制代碼 代碼如下:


<?php
interface 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;
?>


看完上述內(nèi)容,你們對在PHP5中實現(xiàn)多態(tài)的方法有哪些有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

分享標題:在PHP5中實現(xiàn)多態(tài)的方法有哪些-創(chuàng)新互聯(lián)
標題路徑:http://muchs.cn/article10/ddcigo.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃軟件開發(fā)、企業(yè)網(wǎng)站制作企業(yè)建站、Google、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務器托管