//在这里使用重载实现speedDown方法
public function __call($name,$args){
if ($name == 'speedDown') {
$this->speed -=10;
}
}
}//不太懂。先过一遍吧
public function __call($name,$args){
if ($name == 'speedDown') {
$this->speed -=10;
}
}
}//不太懂。先过一遍吧
2015-07-21
class Truck extends Car {
public function speedUp(){
$this->speed=parent::speedUp()+50;
}
}
public function speedUp(){
$this->speed=parent::speedUp()+50;
}
}
2015-07-21
protected function speedUp() {
$this->speed += 10;
}
//增加start方法,使他能够调用受保护的方法speedUp实现加速10
public function start(){
$this->speedUp();
}// 理解的是:在同一个类里,可以直接调用。不需要self
public function start(){
self::speedUp();
}
}
$this->speed += 10;
}
//增加start方法,使他能够调用受保护的方法speedUp实现加速10
public function start(){
$this->speedUp();
}// 理解的是:在同一个类里,可以直接调用。不需要self
public function start(){
self::speedUp();
}
}
2015-07-21
<?php
class Car {
private static $speed = 10;
public function getSpeed() {
return self::$speed;
}
//在这里定义一个静态方法,实现速度累加10
public static function speedUp(){
return self::$speed+=10;
}
class Car {
private static $speed = 10;
public function getSpeed() {
return self::$speed;
}
//在这里定义一个静态方法,实现速度累加10
public static function speedUp(){
return self::$speed+=10;
}
2015-07-21
<?php
class Car {
//增加构造函数与析构函数
function __construct(){
print "construct \n";
}
function __destruct(){
print "destruct \n";
}
}
$car = new Car();
class Car {
//增加构造函数与析构函数
function __construct(){
print "construct \n";
}
function __destruct(){
print "destruct \n";
}
}
$car = new Car();
2015-07-21
<?php
class Car {
public $speed = 0;
//增加speedUp方法,使speed加10
public function speedUp(){
return $this->speed+=10;
}
}
$car = new Car();
$car->speedUp();
echo $car->speed;
class Car {
public $speed = 0;
//增加speedUp方法,使speed加10
public function speedUp(){
return $this->speed+=10;
}
}
$car = new Car();
$car->speedUp();
echo $car->speed;
2015-07-21
<?php
class Car{
//在这里定义一个共有属性name
public $name = 'name';
}
$car = new Car();
//在这里输出$car对象的name属性
echo $car->name;
class Car{
//在这里定义一个共有属性name
public $name = 'name';
}
$car = new Car();
//在这里输出$car对象的name属性
echo $car->name;
2015-07-21
//定义方法
public function getName() {
//方法内部可以使用$this伪变量调用对象的属性或者方法
return $this->name;
public function getName() {
//方法内部可以使用$this伪变量调用对象的属性或者方法
return $this->name;
2015-07-21