php开发教程之protected受保护的
php开发教程之protected受保护的protected:受保护的,在整个继承链上使用例题://例题一:
<?php
class A {
protected $num=10; //在整个继承链上访问
}
class B extends A {
public function getNum() {
echo $this->num;
}
}
//测试
$obj=new B(); //整个继承链上有A和B
$obj->getNum(); //10
//例题二:
<?php
class A {
public function getNum() {
echo $this->num;
}
}
class B extends A {
protected $num=10;
}
//测试
$obj=new B(); //整个继承链上有A和B
$obj->getNum(); //10
//例题三:
<?php
class A {
public function getNum() {
echo $this->num;
}
}
class B extends A {
protected $num=10;
}
//测试
$obj=new A(); //整个继承链上只有A
$obj->getNum(); //Notice: Undefined property: A::$num
页:
[1]