class Message {
protected $body;
public function setBody($body)
{
$this->body = $body;
}
public function getBody()
{
return $this->body;
}
}
class ExtendedMessage extends Message {
private $some;
public function __construct($somedata)
{
$this->some = $somedata;
}
public function getBody()
{
return $this->some;
}
}
$a = new Message;
$b = new ExtendedMessage('text');
$a->getBody(); // NULL`
$b->getBody(); // text`
问问题
61 次
1 回答
0
如果 $a 构造消息类,则 $body 为 NULL 因为没有设置任何内容,并且没有 __construct() 触发(不存在)并将 $body 设置为某些东西。
1 将 body 设置为某个值,否则它将始终为 NULL
$a = new Message;
$a->setBody("my text here");
$a->getBody(); // returns "my text here"
2 向消息类添加构造函数
class Message {
protected $body;
public function __construct($a="") {
$this->body = $a;
}
// ur code as shown
}
比跑
$a = new Message("my text");
$a->getBody(); // returns "my text"
$b = new Message;
$b->getBody(); // returns "" - emtpy string
3 将消息类中的正文设置为def类中的空字符串
protected $body = ""; // or whatever u want
它将在使用 getBody() 构造类后返回 val
于 2014-11-12T18:25:09.053 回答