Rumah > pembangunan bahagian belakang > tutorial php > php组合,php排列组合算法_PHP教程

php组合,php排列组合算法_PHP教程

WBOY
Lepaskan: 2016-07-12 08:53:17
asal
1105 orang telah melayarinya

php组合,php排列组合算法

为了提高代码的复用性,降低代码的耦合(组合实现的两种方式)

模式一:

<span> 1</span> <?<span>php
</span><span> 2</span> <span>//</span><span>组合模式一</span>
<span> 3</span> <span>class</span><span> Person{
</span><span> 4</span>     <span>public</span> <span>function</span><span> eat(){
</span><span> 5</span>         <span>echo</span> "eat.<br/>"<span>;
</span><span> 6</span> <span>    }
</span><span> 7</span> <span>}
</span><span> 8</span> 
<span> 9</span> <span>class</span><span> Phone{
</span><span>10</span>     <span>public</span> <span>function</span><span> call(){
</span><span>11</span>         <span>echo</span> "phone call.<br/>"<span>;
</span><span>12</span> <span>    }
</span><span>13</span> <span>}
</span><span>14</span> 
<span>15</span> <span>//</span><span>学生也需要call()这个方法,为了提高代码的复用性(组合)</span>
<span>16</span> <span>class</span> Student <span>extends</span><span> Person{
</span><span>17</span>     <span>private</span> <span>$people</span><span>;
</span><span>18</span>     <span>public</span> <span>function</span><span> learning(){
</span><span>19</span>         <span>echo</span> "learn.<br/>"<span>;
</span><span>20</span> <span>    }
</span><span>21</span>     <span>public</span> <span>function</span> func(<span>$class</span>, <span>$method</span>){<span>//</span><span>兼容多个类的多个方法</span>
<span>22</span>         <span>$this</span>->people = <span>new</span> <span>$class</span><span>;
</span><span>23</span>         <span>$this</span>->people-><span>$method</span><span>();
</span><span>24</span> <span>    }
</span><span>25</span> <span>}
</span><span>26</span> 
<span>27</span> <span>$student</span> = <span>new</span><span> Student();
</span><span>28</span> <span>$student</span>-><span>eat();
</span><span>29</span> <span>$student</span>->func('Phone', 'call'<span>);
</span><span>30</span> <span>$student</span>->learning();
Salin selepas log masuk

模式二:

php组合,php排列组合算法_PHP教程 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

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1125075.htmlTechArticlephp组合,php排列组合算法 为了提高代码的复用性,降低代码的耦合(组合实现的两种方式) 模式一: 1 ? php 2 // 组合模式一 3 class Person{...
Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan