欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          php構(gòu)造方法和java構(gòu)造方法有什么區(qū)別

          區(qū)別:1、重寫子類構(gòu)造函數(shù)時,PHP不調(diào)用父類,而java默認(rèn)在第一個語句前調(diào)用父類構(gòu)造方法;2、Java允許有多個構(gòu)造方法,而PHP值允許有一個構(gòu)造方法;3、Java中的構(gòu)造方法是必須的,而PHP中的不是。

          php構(gòu)造方法和java構(gòu)造方法有什么區(qū)別

          本文操作環(huán)境:Windows10系統(tǒng)、PHP7.1版、Dell G3電腦。

          php構(gòu)造方法和java構(gòu)造方法有什么區(qū)別

          早期的PHP是沒有面向?qū)ο蠊δ艿?,但是隨著PHP發(fā)展,從PHP4開始,也加入了面向?qū)ο蟆HP的面向?qū)ο笳Z法是從JAVA演化而來,很多地方類似,但是又發(fā)展出自己的特色。以構(gòu)造函數(shù)來說,PHP4中與類同名的函數(shù)就被視為構(gòu)造函數(shù)(與JAVA一樣),但是PHP5中已經(jīng)不推薦這種寫法了,推薦用__construct來作為構(gòu)造函數(shù)的名稱。

          1.重寫子類構(gòu)造函數(shù)的時候,PHP會不調(diào)用父類,JAVA默認(rèn)在第一個語句前調(diào)用父類構(gòu)造函數(shù)

          JAVA

          class Father{     public Father(){         System.out.println("this is fahter");     } } class Child extends Father{     public Child(){         System.out.println("this is Child");     } } public class Test {     public static void main(String[] args){         Child c = new Child();     } }

          輸出結(jié)果:

          this is fahter

          this is Child

          <?php class Father{     public function __construct(){         echo "正在調(diào)用Father";     } } class Child extends Father{     public function __construct(){         echo "正在調(diào)用Child";     } } $c = new Child();

          輸出結(jié)果:

          正在調(diào)用Child

          2.重載的實現(xiàn)方式

          JAVA允許有多個構(gòu)造函數(shù),參數(shù)的類型和順序各不相同。PHP只允許有一個構(gòu)造函數(shù),但是允許有默認(rèn)參數(shù),無法實現(xiàn)重載,但是可以模擬重載效果。

          JAVA代碼

          class Car{     private String _color;     //設(shè)置兩個構(gòu)造函數(shù),一個需要參數(shù)一個不需要參數(shù)     public Car(String color){         this._color = color;     }          public Car(){         this._color = "red";     }          public String getCarColor(){         return this._color;     } } public class TestCar {     public static void main(String[] args){         Car c1 = new Car();         System.out.println(c1.getCarColor());         //打印red                  Car c2 = new Car("black");         System.out.println(c2.getCarColor());         //打印black     } }

          PHP代碼

          <?php class Car{     private $_color;     //構(gòu)造函數(shù)帶上默認(rèn)參數(shù)     public function __construct($color="red"){         $this->_color = $color;     }     public function getCarColor(){         return $this->_color;     } } $c1 = new Car(); echo $c1->getCarColor(); //red $c2 = new Car('black'); echo $c2->getCarColor(); //black

          3.JAVA中構(gòu)造函數(shù)是必須的,如果沒有構(gòu)造函數(shù),編譯器會自動加上,PHP中則不會。

          4.JAVA中父類的構(gòu)造函數(shù)必須在第一句被調(diào)用,PHP的話沒有這個限制,甚至可以在構(gòu)造函數(shù)最后一句后再調(diào)用。

          5.可以通過this()調(diào)用另一個構(gòu)造函數(shù),PHP沒有類似功能。

          class Pen{     private String _color;     public Pen(){              this("red");//必須放在第一行     }          public Pen(String color){         this._color = color;     } }

          推薦學(xué)習(xí):《PHP視頻教程》

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