復制代碼 代碼如下:
<?php
class test {
public function __construct() {
}
public function name() {
$this->xname('John');
}
private function showName($name) {
echo 'my name in test is '.$name;
}
}
class extendTest extends test {
public function __construct() {
parent::__construct();
}
private function showName($name) {
echo 'my name in extendTest is '.$name;
}
}
$test = new extendTest();
$test->name();
?>
復制代碼 代碼如下:
class Person{
protected $name;//protected保護的權限,在子類可以訪問,外部不能訪問
protected $age;
protected $sex;
function __construct($name,$age,$sex){
$this->name=$name;//當使用this時,就算name沒有聲明,也會再次聲明一個
$this->age=$age;
$this->sex=$sex;
echo "###############";
}
public function say(){
echo "我的名字:{$this->name},我的年齡{$this->age}:,我的性別:{$this->sex}<br/>";
}
protected function eat(){
echo "wwwwwwwwwwwwwwwwwwwww<br>";
}
function run(){
}
protected $name;//protected保護的權限,在子類可以訪問,外部不能訪問
protected $age;
protected $sex;
}
//繼承
class Student extends Person{
var $school;
function __construct($name,$age,$sex,$school){
parent::__construct();//調用父類的構造方法
$this->school=$school;
}
//重載say()方法,進行擴展
protected function say(){//父類使用public,子類的權限不能低于父類,可以喝父類的權限相同
//Person::say();//調用父類的say()方法
parent::say();//調用父類say()方法,parent代表父類名,當父類名變化時也能夠調用。
echo "我所在的學校{$this->school}<br/>";//www.3ppt.com
}
function study(){
echo "{$this->name}在學習<br/>";
}
}
$s=new Student("zhangsan",23,"男");
$s->say();
$s->study();
新聞熱點
疑難解答