首页上一页 1 下一页尾页 1 条记录 1/1页
关于例子中成员变量设置的小问题请教
发表在PHP图书答疑
2014-07-31
是否精华
是
否
版块置顶:
是
否
老师们好,我在学习PHP入门到精通的面向对象的继承性时,例子中有一个小地方看不懂,特来请教:
问题描述:为什么在子类BeatBasketBall中,只声明了成员变量$height,而不声明$name呢?代码如下:
<?php
/* * * * * * 演 示 类 的 继 承 性 * * * * *
*
*
*
*
*/
/* 父类 */
class SportObject{
public $name;
public $age;
public $avoirdupois;
public $sex;
public function __construct($name,$age,$avoirdupois,$sex){
$this->name = $name;
$this->age = $age;
$this->avoirdupois = $avoirdupois;
$this->sex = $sex;
}
function showMe(){
echo "这句话不显示";
}
}
/* 子类 BeatBasketBall*/
class BeatBasketBall extends SportObject{
public $height;/*??? 这里看不懂,为什么只声明成员变量$height,而不声明成员变量$name */
function __construct($name,$height){
$this->height = $height;
$this->name = $name;
}
function showMe(){
if($this->height>185){
return $this->name.",符合打篮球的要求";
}else{
return $this->name.",不符合打篮球";
}
}
}
/* 子类 WeightLifting */
class WeightLifting extends SportObject{
function showMe(){
if($this->avoirdupois){
return $this->name.",符合举重要求";
}else{
return $this->name.",不适合举重";
}
}
}
/*实例化对象*/
$BeatBasketBall = new BeatBasketBall('科技','190');
$WeightLifting = new WeightLifting('明日','185','20','男');
echo $BeatBasketBall->showMe()."<br/>";
echo $WeightLifting->showMe()."<br/>";
?>
问题描述:为什么在子类BeatBasketBall中,只声明了成员变量$height,而不声明$name呢?代码如下:
<?php
/* * * * * * 演 示 类 的 继 承 性 * * * * *
*
*
*
*
*/
/* 父类 */
class SportObject{
public $name;
public $age;
public $avoirdupois;
public $sex;
public function __construct($name,$age,$avoirdupois,$sex){
$this->name = $name;
$this->age = $age;
$this->avoirdupois = $avoirdupois;
$this->sex = $sex;
}
function showMe(){
echo "这句话不显示";
}
}
/* 子类 BeatBasketBall*/
class BeatBasketBall extends SportObject{
public $height;/*??? 这里看不懂,为什么只声明成员变量$height,而不声明成员变量$name */
function __construct($name,$height){
$this->height = $height;
$this->name = $name;
}
function showMe(){
if($this->height>185){
return $this->name.",符合打篮球的要求";
}else{
return $this->name.",不符合打篮球";
}
}
}
/* 子类 WeightLifting */
class WeightLifting extends SportObject{
function showMe(){
if($this->avoirdupois){
return $this->name.",符合举重要求";
}else{
return $this->name.",不适合举重";
}
}
}
/*实例化对象*/
$BeatBasketBall = new BeatBasketBall('科技','190');
$WeightLifting = new WeightLifting('明日','185','20','男');
echo $BeatBasketBall->showMe()."<br/>";
echo $WeightLifting->showMe()."<br/>";
?>