php入門教程之物件導向

來源:互聯網
上載者:User

第一種情況:子類沒有定義建構函式時,預設繼承。

第二種情況:子類定義了建構函式,則不會被繼承。

對於4.x,如果父類恰好定義了子類的同名函數,則會被當做子類的建構函式:

 代碼如下 複製代碼

class A
{
    function A()
    {
      echo "I am the constructor of A.<br>n";
    }

    function B()
    {
        echo "I am a regular function named B in class A.<br>n";
        echo "I am not a constructor in A.<br>n";
    }
}

class B extends A
{
    function C()
    {
        echo "I am a regular function.<br>n";
    }
}

//php4 will call B()
$b = new B;

上面的代碼在php5中則會調用A而不會調用B()

PHP物件導向:介面執行個體

們設計一個線上銷售系統,使用者部分設計如下:

將使用者分為,NormalUser, VipUser, InnerUser三種。

要求根據使用者的不同折扣計算使用者購買產品的價格。

並要求為以後擴充和維護預留空間。

使用者部分先聲明了一個介面User,使用者都是User的實現。

user.php

 代碼如下 複製代碼

<?
/*
* 定義了 User介面.
* 和子類 NormalUser,VipUser,InnerUser
*/
//User介面,定義了三個抽象方法.
interface User{
 public function getName();
 public function setName($_name);
 public function getDiscount();
}
abstract class AbstractUser implements User{
 private $name = ""; //名字
 protected  $discount = 0; //折扣
 protected  $grade = "";  //層級
 
 public function __construct($_name){
  $this->setName($_name);
 }
 public function getName(){
  return $this->name;
 }
 public function setName($_name){
  $this->name = $_name;
 }
 public function getDiscount(){
  return $this->discount;
 }
 
 public function getGrade(){
  return $this->grade;
 }
}
class NormalUser extends AbstractUser  { 
 protected  $discount = 1.0;
 protected  $grade = "NormalUser";
}

class VipUser extends AbstractUser {
 protected  $discount = 0.8;
 protected  $grade = "VipUser";
}

class InnerUser extends AbstractUser {
 protected  $discount = 0.7;
 protected  $grade = "InnerUser";
}
?>

關於產品,我們進行了如下設計。

聲明一個介面Product,然後從Product繼承下Book介面。

線上銷售的圖書最後是實現了Book介面的BookOnline類。

Product.php

 代碼如下 複製代碼

<?
/*與產品相關的類放.*/
Interface Product{ //定義產品介面
 public function getProductName();
 public function getProductPrice();
}

interface Book extends Product { // book是產品的一個分類
 public function getAuthor();
}

class BookOnline implements Book{ // 定義book類.
 private $productName;  // 產品名
 private $productPrice; // 產品價格
 private $author;  //作者
 
 public function __construct($_bookName){
  $this->productName = $_bookName;
  //這裡放置相關初始化的代碼.
  //與資料庫關聯的代碼.
 }
 
 public function getProductName(){
  return $this->productName;
 }
 
 public function getProductPrice(){
  //這裡從資料庫讀取價格.
  //假設價格是 100元.
  $this->productPrice = 100;
  return $this->productPrice;
 }
 
 public function getAuthor(){
  //從資料庫裡面取值.
  return $this->author;
 } 
}
?>

關於結算,我們使用了獨立的結算類,使用靜態方法做計算。產品結算。注意參數類型。

 代碼如下 複製代碼

<?
include_once("User.php");
include_once("Product.php");
//買了產品到底多少錢呢?
class ProductSettle{
 public static function  finalPrice(User $_user,Product $_product,$number = 1){
  $price = $_user->getDiscount() * $_product->getProductPrice() * $number;
  return $price;
 }
}
?>

下面的例子是實現。大家可以自己分析下。

 代碼如下 複製代碼

<?
include_once("./class/User.php");
include_once("./class/Product.php");
include_once("./class/ProductSettle.php");

$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 .= "購買 $number 本 《 ". $book->getProductName() ;
$str .=  "》的價格是 $price <br><br>";
echo $str;


$user = new vipUser("Tom");
$price = ProductSettle::finalPrice($user,$book,$number);
$str =  "您好,尊敬的使用者 " . $user->getName() . " <br>";
$str .= "您的層級是 ". $user->getGrade() .", <br>";
$str .= "您的折扣是 " . $user->getDiscount() . "<br>";
$str .= "購買 $number 本 《 ". $book->getProductName() ;
$str .=  "》的價格是 $price <br><br>";
echo $str;

$user = new InnerUser("Tom");
$price = ProductSettle::finalPrice($user,$book,$number);
$str =  "您好,尊敬的使用者 " . $user->getName() . " <br>";
$str .= "您的層級是 ". $user->getGrade() .", <br>";
$str .= "您的折扣是 " . $user->getDiscount() . "<br>";
$str .= "購買 $number 本 《 ". $book->getProductName() ;
$str .=  "》的價格是 $price <br><br>";
echo $str;
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.