php笔记(四)PHP类和对象之对象继承

建立一个Truck类,扩展Car类,并覆盖speedUp方法,使速度累加50

<?php
class Car {
public $speed = 0; //汽车的起始速度是0

public function speedUp() {
$this->speed += 10;
return $this->speed;
}
}
//定义继承于Car的Truck类
class Truck extends Car{
public function speedUp(){
$this->speed=parent::speedUp()+50;
}
}

$car = new Truck();
$car->speedUp();
echo $car->speed;

 

posted @ 2016-11-28 20:47  rookieM  阅读(165)  评论(0编辑  收藏  举报