我正在尝试编写程序,但是遇到一些错误.
I am trying to write to write a program, but am getting some errors.
基础汽车课
Camaro汽车课
代码方案: 在此示例中,我需要创建一个Camaro实例并将其驱动,我将假定它沿直线运动并且没有其他驱动因素.汽车将一直加速直到达到最大速度.要求驱动器将调用加速.要求加速将当前速度增加1.一旦Camaro达到最大速度,它应该停止加速并打印为达到汽车的最大速度.然后,驱动器的执行也应停止.*
Code Scenario: In this example I need to create an instance of Camaro and tell it to drive, I will assume it’s moving in a straight line and there are no other driving factors. The car will accelerate until it hits its max speed. It is required that drive will call accelerate. It is required accelerate will increment the current speed by 1. Once the Camaro reaches max speed it should stop accelerating and print that it hit the cars max speed. The execution of drive should then also stop.*
我的代码低于我尝试的值.
My Code is below which I tried.
<?php class Car extends CI_Controller { public function accelerate($_brand,$_max) { if($this->$_speed<=$_max) { $this->$_speed += 1; return true; } else { echo $this->_brand . 'Reached max speed'; } function drive() { $this->accelerate(); } } public $_speed = 0; public $_brand = 'unknown'; public $_max = 0; } class Camaro extends Car { public $_brand = 'Chevy'; public $_max = 100; } $car1 = new Camaro(); echo $car1 -> accelerate($_brand,$_max); ?>推荐答案
只需编辑以下代码:
1)在汽车课上:
if($this->_speed<=$_max) { $this->_speed += 1; return true; }2)演示
$car1 = new Camaro(); echo $car1->accelerate($car1->_brand, $car1->_max);更多推荐
程序中未定义的变量
发布评论