程序中未定义的变量

编程入门 行业动态 更新时间:2024-10-25 02:25:05
本文介绍了程序中未定义的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

基础汽车课

  • 当前速度(属性)–默认值0
  • 加速(方法)
  • 驱动器(方法)
  • 品牌(财产)-默认值为未知"
  • 最大速度(属性)-默认值0
  • Camaro汽车课

  • 继承基础车
  • 品牌(财产)-默认值雪佛兰"
  • 最大速度(属性)–默认值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);

    更多推荐

    程序中未定义的变量

    本文发布于:2023-11-11 10:47:18,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1578206.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:变量   中未   定义   程序

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!