Blogger Information
Blog 22
fans 1
comment 1
visits 22865
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
对象之父类子类方法重载
forever浅笑
Original
745 people have browsed it

222.gif

实例

<?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() . '元';

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post