久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      php怎么實現(xiàn)多態(tài)

      在PHP5中,變量的類型是不確定的,一個變量可以指向任何類型的數(shù)值、字符串、對象、資源等。我們無法說PHP5中多態(tài)的是變量。

      我們只能說在PHP5中,多態(tài)應用在方法參數(shù)的類型提示位置。

      php怎么實現(xiàn)多態(tài)

      一個類的任何子類對象都可以滿足以當前類型作為類型提示的類型要求。

      所有實現(xiàn)這個接口的類,都可以滿足以接口類型作為類型提示的方法參數(shù)要求。

      簡單的說,一個類擁有其父類、和已實現(xiàn)接口的身份。

      通過實現(xiàn)接口實現(xiàn)多態(tài)(推薦學習:PHP編程從入門到精通)

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

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號