批改状态:未批改
老师批语:
这是父类:
<?php
class Car
{
//受保护的类
protected $brand;//品牌
protected $price;//价格
//构造方法
public function __construct($brand,$price)
{
$this->brand = $brand;
$this->price = $price;
}
//speed方法
public function speed()
{
return '我是父类';
}
}点击 "运行实例" 按钮查看在线实例
这是子类:
<?php
class SmartCar extends Car
{
//私有属性
private $autopilot = false;
//构造方法
public function __construct($brand, $price,$autopilot)
{
//父类的构造方法
parent::__construct($brand, $price);
$this->autopilot = $autopilot;
}
//魔术方法
public function __get($name)
{
return $this->$name;
}
//导航方法
public function navigation()
{
return '我是子类的方法';
}
//父类的方法重写
public function speed()
{
return parent::speed().'+这是父类的重写';
}
}点击 "运行实例" 按钮查看在线实例
这是测试脚本:
<meta charset="UTF-8">
<?php
spl_autoload_register(function ($classname){
require './class/'.$classname.'.php';
});
$smartcar = new SmartCar('Benz','1000万',false);
echo $smartcar->brand;
echo $smartcar->price;
echo ($smartcar->autopilot?'支持':'不支持');
echo $smartcar->speed();
echo $smartcar->navigation();点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号