【php开源代码栏目提醒】:以下是网学会员为您推荐的php开源代码-poly2.php,希望本篇文章对您学习有所帮助。
<?php
class Fruit{
protected $color;
public function __construct(){
echo 'This is fruit.<br><br>';
}
public function grow(){
}
}
class Apple extends Fruit{
public function __construct(){
$this->color = 'red';
echo $this->shape;
echo 'This is apple.<br><br>';
}
public function grow(){
echo 'Apple grown mothed.<br><br>';
}
}
class Banana extends Fruit{
public function __construct(){
$$this->color = 'yelow';
echo 'This is banana.<br><br>';
}
public function grow(){
echo 'Banana grown mothed.<br><br>';
}
}
class Orange extends Fruit{
public function __construct(){
$$this->color = 'green';
echo 'This is orange.<br><br>';
}
public function grow(){
echo 'Orange grown mothed.<br><br>';
}
}
function getFruit(Fruit $fruit){
if($fruit instanceof Apple)
echo 'Apple类<br><br>';
else if($fruit instanceof Banana)
echo 'Banana类<br><br>';
else if($fruit instanceof Orange)
echo 'Orange类<br><br>';
else
echo 'unknown';
}
$apple = new Apple();
$banana = new Banana();
$orange = new Orange();
getFruit($banana );
$apple->grow();
$banana->grow();
$orange->grow();
?>