php 類介面繼承練習

來源:互聯網
上載者:User

標籤:

  1 <?php  2 /**  3 * @hypo  4  介面的特性:介面中定義的所有方法都必須是public   5  介面的實現:一個介面可以使用implements操作符,類中必須實現介面中的所有方法,否則會報fatal錯誤,如果要實現多個介面,可以使用逗號來分隔多個介面的名稱。  6 */  7 header("Content-type:text/html;charset=utf-8");  8 interface iTemplate    9 {   10     public function setVariable($name, $var);   11     public function getHtml($template);   12 }   13 class Template implements iTemplate   14 {   15     private $vars = array();   16      17     public function setVariable($name, $var)   18     {   19         $this->vars[$name] = $var;   20     }   21      22     public function getHtml($template)   23     {   24         foreach($this->vars as $name => $value) {   25             echo "\n".$name."\n".$value."<br>"; 26         }   27     28       29     }   30 } 31 // $t=new Template(); 32 // $t->setVariable("ji","100"); 33 // $t->setVariable("ji2","200"); 34 // $t->getHtml(‘aa‘); 35 ?> 36 <?php 37 //定義一介面,實現價格折扣 38 interface zk{ 39     public function jiage();//價格 40     public function user_s();//使用者 41 } 42 class ordUser implements zk{//普通使用者.必須實現介面所有 43     private $zkl="0.97"; 44     function jiage(){ 45         return $this->zkl; 46     } 47     function user_s(){ 48         return ‘普通使用者‘; 49     } 50 } 51  52 class vipUser implements zk{//VIP使用者.必須實現介面所有 53     private $zkl="0.91";//折扣 54     function jiage(){ 55         return $this->zkl; 56     } 57     function user_s(){ 58         return ‘vip使用者‘; 59     } 60 } 61  62 class svipUser implements zk{//超級vip使用者.必須實現介面所有 63     private $zkl="0.88"; 64     function jiage(){ 65         return $this->zkl; 66     } 67     function user_s(){ 68         return ‘svip使用者‘; 69     } 70 } 71 class run{//價格折扣計算 72     var $yjg; 73     var $us; 74     function buy($yjg,zk $us){//zk介面名,可以去掉沒影響 75         $this->yjg=$yjg; 76         $this->us=$us; 77         $zjg=$this->us->jiage(); 78         $user=$this->us->user_s(); 79         echo $user.‘:原價格‘.$yjg.‘,現價格‘.$zjg*$yjg."<br>"; 80  81     } 82  83 } 84  85 class hdPrc extends run{//繼承run 顯示不同使用者價格 86     function sp($hd){ 87         $this->buy(active::showActivePrc($hd),new ordUser); 88         $this->buy(active::showActivePrc($hd),new vipUser); 89         $this->buy(active::showActivePrc($hd),new svipUser); 90     } 91      92 } 93 class active{//靜態活動價格添加,顯示,器 94     public static $aName; 95     public static $aPrc; 96     public static function addActive($aNam,$aPr){ 97          self::$aName[$aNam]=$aPr;         98     }     99     public static function showActivePrc($aNam){100         return  self::$aName[$aNam];101     }102 }103 104 active::addActive("電影活動","111");105 active::addActive("音樂活動","1122");106 107 // $t=new run;108 // $t->buy(active::showActivePrc("音樂活動"),new ordUser);109 // $t->buy(‘111‘,new vipUser);110 // $t->buy(‘111‘,new svipUser);111 $b=new hdPrc;112 $b-> sp(‘音樂活動‘);113 ?>

 

php 類介面繼承練習

聯繫我們

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