日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
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會(huì)不調(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ù),編譯器會(huì)自動(dòng)加上,PHP中則不會(huì)。

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;
    }
}

標(biāo)題名稱:php構(gòu)造方法和java構(gòu)造方法有什么區(qū)別
本文地址:http://m.5511xx.com/article/dpdecgc.html