//people.class.php class People{ private $name; private $sex; private $birthday; private function construct($name='',$sex='01',$birthday='1999-01-01'){ echo 'people---construct<br>'; $this->name = $name; $this->sex = $sex; $this->birthday = $birthday; } public function get($key){ return $this->$key; } public function set($value,$key){ $this->$key = $value; } public function show(){ return 'people---'; } }
//student.class.php class Student extends People{ private $s_num; private $s_class; public function construct($name,$sex,$birthday,$num,$class){ //parent::construct($name,$sex,$birthday); echo 'Student--construct<br>'; $this->name = $name; $this->sex = $sex; $this->birthday = $birthday; $this->s_num = $num; $this->s_class = $class; } public function showInfo(){ return 'sutdent---'.$this->name.'----bir='.$this->birthday .'num=='.$this->s_num.'----class=='.$this->s_class; } }
以上两个类Student继承People类
父类的构造方法是私有的,这在java里意味着这个类是无法被继承的,但是在php里,这个类可以被继承,但是有一点,就是子类Student中无法调用父类的构造方法
parent::construct($name,$sex,$birthday);
否则会报出错误,并且,如果父类的构造方法私有,那么子类就必须有自己的构造方法,必须明确写出来,不然是无法实现继承的。
同时和java不同的是,当子类继承父类,并且子类有自己的构造函数的时候,父类的构造函数是不会被执行的,除非在子类的构造函数中调用。
以上是php面向对象继承方面的几点总结的详细内容。更多信息请关注PHP中文网其他相关文章!