实例
<?php /* * 皇帝类 */ class Emperor { private $wife = '皇后'; private $money = 10000000; public function showWife() { echo $this->wife; } public function __get($name) { return $this-$name; } public function getMoney() { return $this->money; } } /* * 太子类 */ class Prince extends Emperor { private $wife = '太子妃'; protected $money = 1000; // 自已的钱 public function showWife() { return $this->wife; } public function showMoney() { return $this->money ; } } $p = new Prince(); //echo $p->wife; // Notice: Undefined property: Prince::$wife 太子不能继承老子的老婆 echo '<hr>'; echo '太子只能使用' . $p->showWife(); // 只能使用自已的太子妃 echo '<hr>'; echo '太子自已的财产' . $p->showMoney() . '元'; echo '<hr>'; echo '太子可以继承老子的财产' . $p->getMoney() . '元';
运行实例 »
点击 "运行实例" 按钮查看在线实例