复制代码 代码如下:
<?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();
新闻热点
疑难解答