Rumah >pembangunan bahagian belakang >tutorial php > 帮忙看上这段代码,为何没输出
帮忙看下这段代码,为何没输出
/*父类*/
class MyObject{
public $object_name;
public $object_price;
public $object_num;
public $object_agio;
function __construct($name,$price,$num,$agio){
$this->object_name=$name;
$this->object_price=$price;
$this->object_num=$num;
$this->object_agio=$agio;
}
function showMe(){
//echo'这句话不会显示。';
}
}
/*子类Book*/
class Book extends MyObject{
public $book_type;
function __construct($type,$num){
$this->book_type=$type;
$this->object_num=$num;
}
function _showMe(){
return'本次新进'.$this->book_type.'图书'.$this->object_num.'本
';
}
}
/*子类Elec*/
class Elec extends MyObject{
function showMe(){
return'热卖图书:'.$this->object_name.'
原价:'.$this->object_price.'
特价:'.$this->object_price*$this->object_agio;
}
}
/*实例化对象*/
$c_book=new Book('计算机类',1000);
$h_elec=new Elec('PHP函数参考大全',98,3,0.8);
echo $c_book->showMe()."
";
//echo $h_elec->showMe();
能输出的语句已经注释,没注释的那句echo $c_book->showMe()."
";
为何没有输出。还有就是‘这句话不会显示’不注释时为何还会输出?多谢!
------解决方案--------------------
Book::_showMe() 方法的确可以输出,但你调用的是 showMe() ,这个方法是继承自MyObject得来的,而你又注释掉了echo ,当然没有任何输出。
------解决方案--------------------