程序中未定义的变量 [英] Undefined variable in program

查看:97
本文介绍了程序中未定义的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写程序,但是遇到一些错误.

I am trying to write to write a program, but am getting some errors.

基础汽车课

  1. 当前速度(属性)–默认值0
  2. 加速(方法)
  3. 驱动器(方法)
  4. 品牌(财产)-默认值为未知"
  5. 最大速度(属性)-默认值0

Camaro汽车课

  1. 继承基础车
  2. 品牌(财产)-默认值雪佛兰"
  3. 最大速度(属性)–默认值200

代码方案: 在此示例中,我需要创建一个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);

这篇关于程序中未定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆