久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      php是多繼承還是單繼承

      php是單繼承。php是不支持多繼承的,但是php可以通過使用interface或者trait實(shí)現(xiàn)多繼承,如【interface test1 {public function connect();}interface test2 …】。

      php是多繼承還是單繼承

      本文操作環(huán)境:windows10系統(tǒng)、php 7、thinkpad t480電腦。

      php是單繼承還是多繼承?可能你也被這個(gè)問題困惑很久了吧。首先,PHP是單繼承,他是不支持多繼承的。面向?qū)ο蟮奶攸c(diǎn)是封裝、繼承、多態(tài)。這里的繼承是指類與類之間的繼承關(guān)系,可以使用關(guān)鍵字extends實(shí)現(xiàn),這里只可以繼承一個(gè)類。

      那么,php是否可以實(shí)現(xiàn)多繼承呢?答案是可以的。

      php可以用兩種方式實(shí)現(xiàn)多繼承。一個(gè)是使用interface實(shí)現(xiàn)。還有一個(gè)就是使用trait實(shí)現(xiàn)。

      第一種:關(guān)于使用interface實(shí)現(xiàn),它的原理就是一個(gè)類可以實(shí)現(xiàn)多個(gè)接口,我們可以定義多個(gè)接口類,如下:

      interface test1 { 	public function connect(); }
      interface test2 { 	public function contact(); }

      一個(gè)類實(shí)現(xiàn)多個(gè)接口:

      class MyClass implements test1,test2 { 	public function connect() { 		echo "test1"; 	} 	public function contact() { 		echo "test2"; 	} }

      第二種:使用trait實(shí)現(xiàn)。其實(shí)trait并不算是多繼承,應(yīng)該叫做類似多繼承的功能。

      什么是trait呢?

      答:看上去既像類又像接口,其實(shí)都不是,Trait可以看做類的部分實(shí)現(xiàn),可以混入一個(gè)或多個(gè)現(xiàn)有的PHP類中,其作用有兩個(gè):表明類可以做什么;提供模塊化實(shí)現(xiàn)。Trait是一種代碼復(fù)用技術(shù),為PHP的單繼承限制提供了一套靈活的代碼復(fù)用機(jī)制。

      //基本類   class basicTest{     public function test(){       echo "hello,worldn";     }   }    //traitOne   trait traitOne{     public function test(){       echo "this is trait one";     }     public function testOne(){       echo "one";     }   }    //traitTwo   trait traitTwo{     // public function test(){       // echo "this is trait two!";     // }      public function testTwo(){       echo "Two";     }   }    //繼承基本類,并use trait   class myCode extends basicTest{     use traitOne,traitTwo;     public function test(){       echo "hehaha!!";     }   }    $obj = new myCode();   $obj->testTwo();

      注意:

      優(yōu)先級(jí):自身方法>trait的方法>繼承的方法(就是這樣子的。)
      如果我們打開上述代碼中的注釋,會(huì)報(bào)錯(cuò),因?yàn)閮蓚€(gè)trait中的方法重名了。

      如何解決trait中方法重名的情況?

      //使用demo1和demo2的方法,但并不是導(dǎo)入命名空間 //demo1和demo2種都有hello方法     use Demo1,Demo2{         //將Demo1的hello方法替換Demo2的hello方法         Demo1::hello insteadof Demo2;         //給Demo2的hello方法起別名         Demo2::hello as Demo2Hello;     } //下方調(diào)用的時(shí)候 return $this->hello(); // 使用demo1的方法 return $this->Demo2Hello();  //使用demo2的方法

      推薦學(xué)習(xí):php培訓(xùn)

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)