这段代码为何会输出father?求合理解释?
<?php
class father
{
public function __construct()
{
$this->init();
} private function init()
{
echo "father\n";
}
}class son extends father
{
public function init()
{
echo "son\n";
}
}$son = new son();
<?php
class father
{
public function __construct()
{
$this->init();
} private function init()
{
echo "father\n";
}
}class son extends father
{
public function init()
{
echo "son\n";
}
}$son = new son();
解决方案 »
- 调用webservice的第二种方式的错误读取是怎样的
- 采集内容入库前的进度条?
- 正则陷阱?
- 有关FCKeditor_2.3.2的问题
- 请教各位大虾在php中怎么读取javascript中的变量
- 菜鸟学php遇到的第二个难题为何函数传不进值呢
- $_GET["str"]为什么这样的形式不能使用?
- 求教在php程序中调用mysqldump问题
- 看谁笑到最后,看谁笑的最美,真诚的祝php内的程序员们中秋快乐,散分,
- 在php中使用sql server 存储过程,可是返回值或者输出参数不知怎么接收,望各位大虾告知,感激不尽!!!
- phpMyAdmin 尝试连接到 MySQL 服务器,但服务器拒绝连接。
- 请问PHP怎么获取地址中图片的参数
需要解释吗?这和所有的权限系统都是一样的
因为你是new son
因为你是new son我知道,我的意思是,既然这个this是指向son的,那么this->init()不应该是调用son的init吗?那么不就是输出son么,为何输出father?
son::init() 是共有的
根据共有的不能覆盖私有的这个原则
father::init() 不会被 son::init() 覆盖
所以在执行 father::__construct() 时,$this->init() 执行的是 father::init()至于为什是 共有的不能覆盖私有的 而不是别的
那就不太好说了,这是约定俗成的东西,没有什么道理可讲(当然也与西方人的价值观有关)