c->c();//报错: Ca">
时间:2021-07-01 10:21:17 帮助过:25人阅读
class a {
var $c;
function aa() {
echo "I'm a!";
}
function gc(){
$this->c = new c;
}
}
class b extends a {
function bb() {
echo "I'm b!";
}
function dc(){
$this->c->c();//报错: Call to a member function c() on a non-object
//在子类中如何使用父类中定义的第三方的类方法呢?
}
}
class c {
function c() {
echo "I'm c!";
}
}
$a = new a;
$b = new b;
$a->aa();//I'm a!
$a->gc();//I'm c!
$b->dc();//报错: Call to a member function c() on a non-object
?>
输出第三个I'm c!
class a {
var $c;
function aa() {
echo "I'm a!";
}
function gc(){
$this->c = new c;
}
}
class b extends a {
function bb() {
echo "I'm b!";
}
function dc(){
parent::gc();//要先实例化c $this->c才有值
//由于 方法c() 和类c 同名,实例化时会自动调用(构造方法)
class a {
public $c;
function aa() {
echo "I'm a!";
}
function gc(){
$this->c = new c;
}
}
class b extends a {
function bb() {
echo "I'm b!";
}
function dc(){
$this->gc(); // 执行gc方法创建c对象
$this->c->c();
}
}
class c {
function c() {
echo "I'm c!";
}
}
$a = new a;
$b = new b;
$a->aa();//I'm a!
$a->gc();//I'm c!
$b->dc();//I'm c!
?>