protected与private在构造函数选择执行时的差异[有点乱,请看实例]

protected与private在构造函数选则执行时的差异[有点乱,请看实例]
<?php
class A{
function __construct(){
   $this->say();
   $this->hello();
  }
protected function say(){
 echo __CLASS__."[A]<br/>";
  }
private function hello(){
  echo "[a]hello<br/>";
  }
}

class B extends A{
function __construct(){
    parent::__construct();
    $this->say();
    $this->hello();
 }
 function say(){
 echo __CLASS__."[B]<br/>";
}
 function hello(){
  echo "[b]hello<br/>";
  }
}
$a=new B();
?>

output:

B[B]
[a]hello
B[B]
[b]hello

 

parent::__construct()执行时 say方法是子类的,而hello类是父类的,差异十分明显
上一篇:剑指offer 排序数组中的两数之和


下一篇:剑指offer 最接近的三数之和