<?php
class?Car?{
????private?$speed?=?0;
????
????public?function?getSpeed()?{
????????return?$this->speed;
????}
????
????protected?function?speedUp()?{
????????$this->speed?+=?10;
????}
????
????//增加start方法,使他能夠調(diào)用受保護(hù)的方法speedUp實(shí)現(xiàn)加速10
????public?function?start(){
????????$this->speedUp();
????}
}
$car?=?new?Car();//創(chuàng)建的對(duì)象里面能夠訪問的方法只有g(shù)etSpeed()和start(),此時(shí)速度為0
$car->start();//調(diào)用方法start(),而start()可以訪問私有方法speedUp(),此時(shí)速度為10
echo?$car->getSpeed();//最后調(diào)用可以訪問的公有方法來獲取當(dāng)前速度,最后輸出為10.
2018-04-27
沒錯(cuò)吧
2018-08-02
protect方法不能被外部類訪問,
$car
?=?
new
?Car()是外部類所以不能調(diào)用類Car的protect方法,記住了class X{}大括號(hào)之外的都是外部類,父類和子類就不一樣,有繼承關(guān)系所以可以調(diào)用