PHP5中實現多態的兩種方法執行個體分享_PHP教程

來源:互聯網
上載者:User
在PHP5中,變數的類型是不確定的,一個變數可以指向任何類型的數值、字串、對象、資源等。我們無法說PHP5中多態的是變數。

我們只能說在PHP5中,多態應用在方法參數的類型提示位置。
一個類的任何子類對象都可以滿足以當前類型作為類型提示的類型要求。
所有實現這個介面的類,都可以滿足以介面類型作為類型提示的方法參數要求。
簡單的說,一個類擁有其父類、和已實現介面的身份。

通過實現介面實現多態
複製代碼 代碼如下:
interface 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 三種。要求根據使用者的不同折扣計算使用者購買產品的價格。並要求為以後擴充和維護預留空間。
複製代碼 代碼如下:
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() . "
";
$str .= "您的層級是" . $user->getGrade() . "
";
$str .= "您的折扣是" . $user->getDiscount() . "
";
$str .= "您的價格是" . $price;
echo $str;
?>

http://www.bkjia.com/PHPjc/756991.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/756991.htmlTechArticle在PHP5中,變數的類型是不確定的,一個變數可以指向任何類型的數值、字串、對象、資源等。我們無法說PHP5中多態的是變數。 我們只能...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.