嗨,我想了解如何inherteince使用面向对象编程在PHP中工作。主要类是计算机,继承的类是鼠标。我正在用鼠标类去开发Computer类。我在每个类中使用__construct,当我istinate类时,我首先使用pc类型,如果它有鼠标后。由于某些原因计算机返回null?为什么是这样?PHP类延伸不工作为什么这是如何正确地扩展一个类?
class Computer {
protected $type = 'null';
public function __construct($type) {
$this->type = $type;
}
public function computertype() {
$this->type = strtoupper($this->type);
return $this->type;
}
}
class Mouse extends Computer {
protected $hasmouse = 'null';
public function __construct($hasmouse){
$this->hasmouse = $hasmouse;
}
public function computermouse() {
if($this->hasmouse == 'Y') {
return 'This Computer has a mouse';
}
}
}
$pc = new Computer('PC', 'Y');
echo $pc->computertype;
echo $pc->computermouse;
没有直接关系的答案,但它会更有意义,如果你的计算机对象有一个鼠标属性,它是一个鼠标类型的对象 – Erik 2011-03-17 00:19:01
@Erik我的评论帮助我走出最多,让我重新思考父类的构建。 – Aaron 2011-03-17 00:57:23