為什么我在父類里定義一下private屬性,通過子類繼承,可在外面訪問,
class Human{
?? ?private?? $height;?? ??? ?
}
class Player extends Human {
??? protected $age;
??? function __construct($name,$height){
?? ??? ? $this->name = $name;
?? ??? ? $this->height = $height;
?? ??? ? }
}
$joden=new Player('joden','25');
echo $joden->height;
2014-12-22
Play的構造方法中這句代碼:
$this->height = $height;
相當于給player增加了一個height屬性...
2014-12-22
class Human{
? ? private ? $height;
? ? public function setHeight($h){
? ? $this->height=$h;
? ?
? ? }
? ? public function printHeight(){
? ? echo "Human height:".$this->height;
? ? }
}
class Player extends Human {
? ? protected $age;
? ? function __construct($name,$height){?
? ? ? ? ?$this->name = $name;
? ? ? ? ?$this->height = $height;
? ? ? ? ?}
}
$joden=new Player('joden','25');
echo 'Player Heihgt:'.$joden->height;
echo '<br>';
echo '調用 Human 方法設置Human 的私有height:<br>';
echo $joden->setHeight(30);
echo $joden->printHeight();
echo '<br>';
echo '再次打印Player的Heihgt:'.$joden->height;