php組合,php排列組合演算法_PHP教程

來源:互聯網
上載者:User

php組合,php排列組合演算法


為了提高代碼的複用性,降低代碼的耦合(組合實現的兩種方式)

模式一:

 1 php 2 //組合模式一 3 class Person{ 4     public function eat(){ 5         echo "eat.
"; 6 } 7 } 8 9 class Phone{10 public function call(){11 echo "phone call.
";12 }13 }14 15 //學生也需要call()這個方法,為了提高代碼的複用性(組合)16 class Student extends Person{17 private $people;18 public function learning(){19 echo "learn.
";20 }21 public function func($class, $method){//相容多個類的多個方法22 $this->people = new $class;23 $this->people->$method();24 }25 }26 27 $student = new Student();28 $student->eat();29 $student->func('Phone', 'call');30 $student->learning();

模式二:

1 php 2 //組合模式二 3 class Person{ 4 public function eat(){ 5 echo "eat.
"; 6 } 7 } 8 9 trait Drive{10 public function call(){11 echo "phone call.
";12 }13 }14 15 class Student extends Person{16 use Drive;17 public function learning(){18 echo "learn.
";19 }20 }21 22 $student = new Student();23 $student->eat();24 25 //當方法或屬性同名時,當前類中的方法會覆蓋 trait的 方法,而 trait 的方法又覆蓋了基類中的方法26 $student->call();27 $student->learning();View Code

http://www.bkjia.com/PHPjc/1125075.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1125075.htmlTechArticlephp組合,php排列組合演算法 為了提高代碼的複用性,降低代碼的耦合(組合實現的兩種方式) 模式一: 1 ? php 2 // 組合模式一 3 class Person{...

  • 聯繫我們

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