av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

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

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

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

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


輸出結(jié)果:

正在調(diào)用Child

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

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

JAVA代碼

class Car{
    private String _color;
    //設(shè)置兩個(gè)構(gòu)造函數(shù),一個(gè)需要參數(shù)一個(gè)不需要參數(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代碼

_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è)限制,甚至可以在構(gòu)造函數(shù)最后一句后再調(diào)用。

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

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

本文名稱:php構(gòu)造方法和java構(gòu)造方法有什么區(qū)別
本文鏈接:http://uogjgqi.cn/article/dpdecgc.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們在微信上24小時(shí)期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流