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