普通网友 2016-08-11 11:08
浏览 40
已采纳

访问父类的随机值

So, let's say I have this dummy code in one file:

<?php
class Dice
{
    public $maxPossibleNo;
    public $secretNumber;

    function __construct($no_of_dice=1) 
    {
        // how many possible dice number to enroll
        $this->maxPossibleNo = $no_of_dice * 6;

        // do shaking dice
        $this->secretNumber = $this->getSecretNumber();
    }

    function getSecretNumber() 
    {
        return rand(1, $this->maxPossibleNo);
    }

    function roll() 
    {
        $found = false;
        list($array1, $array2) = array_chunk(range(1, $this->maxPossibleNo), ceil($this->maxPossibleNo/2));
        /*
            Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
            )
            Array
            (
                [0] => 4
                [1] => 5
                [2] => 6
            )
        */

        $guess = new Guess();
        echo $this->secretNumber;
        echo $guess->checkSecretNumber();
    }
}

class Guess extends Dice
{
    function checkSecretNumber() 
    {
        return $this->secretNumber;
    }

    function isGreaterThan($x) 
    {
        return $x > $this->secretNumber;
    }

    function isLessThan($x) 
    {
        return $x < $this->secretNumber;
    }

    function isEqual($x) 
    {
        return $x == $this->secretNumber;
    }
}

$game = new Dice('1');
$game->roll();

Result:

65
43
54

Expected Result:

66
33
55

I want the Guess class to be able to access the secret number of the Dice class without having to roll it again. So I can manipulate the Guess class with other function.

EDIT:

Expectation flow: The main class will only called once (to generate the secret number), while i will need to do loop checking for the secret number for many times. (I guess the way is to create another class of it, and it will be able to be called repeatly for auto checking purpose, but i had did mistake here and dont have any idea how to correct this part.)

Any suggested correction will be appreciated.

Thank you

  • 写回答

1条回答 默认 最新

  • drrc61668568 2016-08-11 11:29
    关注

    when you create a new Guess(), it runs the constructor of Dice automatically, since Guess extends dice. The secret number in the Guess instance is not the secret number of the first Dice instance, it's a separate instance. I think your design is flawed. Why does Guess need to extend Dice? A guess is not logically a different implementation of a Dice (which is what you'd (logically) normally use subclasses for).

    Here's how I would do it (without knowing anything further about what exactly you want to achieve):

    <?php
    class Dice
    {
      public $maxPossibleNo;
      public $secretNumber;
    
      function __construct($no_of_dice=1) 
      {
        // how many possible dice number to enroll
        $this->maxPossibleNo = $no_of_dice * 6;
    
        // do shaking dice
        $this->secretNumber = $this->getSecretNumber();
      }
    
      function getSecretNumber() 
      {
        return rand(1, $this->maxPossibleNo);
      }
    
      function roll() 
      {
        $found = false;
        list($array1, $array2) = array_chunk(range(1, $this->maxPossibleNo), ceil($this->maxPossibleNo/2));
        /*
            Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
            )
            Array
            (
                [0] => 4
                [1] => 5
                [2] => 6
            )
        */
      }
    }
    
    class Guess
    {
      private $dice;
    
      function __construct(Dice $d)
      {
        $this->dice = $d;
      }
    
      function checkSecretNumber() 
      {
        return $this->dice->secretNumber;
      }
    
      function isGreaterThan($x) 
      {
        return $x > $this->dice->secretNumber;
      }
    
      function isLessThan($x) 
      {
        return $x < $this->dice->secretNumber;
      }
    
      function isEqual($x) 
      {
        return $x == $this->dice->secretNumber;
      }
    }
    
    $dice = new Dice('1');
    $dice->roll();
    $guess = new Guess($dice);
    echo $dice->secretNumber;
    echo $guess->checkSecretNumber();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)