测试PHP的继承与实例化位置

<?
// animal.php
class Animal {
    private $weight;
    public function getWeight()
    {
        return $this->weight;
    }
    public function setWeight($w)
    {
        $this->weight = $w;
    }
}

$myDog = new Dog();
$myDog->setWeight(20);
echo "Mydog's weight is ".$myDog->getWeight().'<br>';
$myDog->Bark();

class Dog extends Animal
{
    /**
    *子类新增方法
    */
    public function Bark()
    {
        echo "Wang~~Wang~~~ ";
    }
}

?>

运行结果:

Mydog's weight is 20
Wang~~Wang~~~

 

posted @ 2013-01-09 11:28  希亚  阅读(167)  评论(0编辑  收藏  举报