如何在php

时间:2018-09-19 01:15:01

标签: php class inheritance

我遇到了以下代码挑战。它是关于接口,类继承等的,不知道该怎么做。这就是我得到的

// interface bird
interface Bird
{
    // lay egg
    public function layEgg();
}

// chicken can lay egg
class Chicken implements Bird
{
    public function layEgg() {
        return new Egg();
    }
}

// class egg
class Egg
{
    public $count=0;

    // egg, bird type
    public function __construct($birdType)
    {

    }

    // hatch, born chick
    public function hatch()
    {
        if($this->count == 0)
            $this->count++;

        if($this->count == 1)
             throw new Exception('lay egg for 2nd time');
    }
}

有4个要求: Chicken实现了Bird界面。

鸡下蛋。

不同的鸟类会相应地产卵。

孵化两次将引发异常。

有什么主意吗?

4 个答案:

答案 0 :(得分:1)

该代码段如何?

<?php

// interface bird
interface Bird
{
    public function layEgg();
}

class Chicken implements Bird {
    public function layEgg (): Egg {
        return new Egg($this);
    }
}

class Egg {

    protected $egg;
    protected $eggBasket = [];

    public function __construct($birdType)
    {
       $this->egg = $birdType;
    }

    public function hatch() : Bird {
        $this->eggBasket[get_class($this->egg)][] = 'Layed one ' . get_class($this->egg) . ' egg';
        if(
            array_key_exists(get_class($this->egg), $this->eggBasket) && 
            count($this->eggBasket[get_class($this->egg)]) > 1) 
        {
            throw new Exception('Cannot hatch twice!');
        }
        return new $this->egg;
    }
}

$chicken = new Chicken();
$egg = $chicken->layEgg();
$bird = $egg->hatch();

答案 1 :(得分:0)

这是我的尝试:

interface BirdInterface
{
    public function layEgg(): AbstractEgg;
}

abstract class AbstractEgg
{
    /**
     * @var BirdInterface|null
     */
    protected $hatchedBird = null;

    protected function hatchCheck() {
        if (!is_null($this->hatchedBird)) {
            throw new \Exception();
        }
    }
}

class Chicken implements BirdInterface
{
    public function layEgg(): AbstractEgg
    {
        return new ChickenEgg();
    }
}

class ChickenEgg extends AbstractEgg
{
    public function hatch(): BirdInterface
    {
        $this->hatchCheck();
        return $this->hatchedBird = new Chicken();
    }
}

https://www.php-fig.org/bylaws/psr-naming-conventions/

已更新为更抽象的版本。

答案 2 :(得分:0)

这是我的尝试,还有一些附加功能:

interface Bird{
// lay egg
   public function layEgg();
   public function printType();
}

 // chicken can lay egg
class Chicken implements Bird{
    public $counter = 0;
    public function layEgg() {
        $this->counter++;
        $egg= new Egg($this);
        return $egg;

    }
    public function printType(){
        echo get_class($this)." egg is layed";
    }
    public function printDescandantNumber(){
        echo $this->counter;
    }
}


// Eagle can lay egg
class Eagle implements Bird{
    public $counter = 0;
    public function layEgg() {
        $this->counter++;
        $egg= new Egg($this);
        return $egg;

    }
    public function printType(){
        echo get_class($this)." egg is layed";
    }
    public function printDescandantNumber(){
        echo $this->counter;
    }
}

// class egg
class Egg {
    public $count=0;
    private $birdtype;
    private $generation;

    // egg, bird type
    public function __construct($birdType){
        $this->birdtype=$birdType;
        $this->generation=$this->birdtype->counter++;
    }

    // hatch, born chick
    public function hatch(){

        if($this->count == 0){
            $this->count++;
            $bird = new $this->birdtype;
            $bird->counter=$this->generation;
            return $bird;
        }


        if($this->count == 1)
            throw new Exception('lay egg for 2nd time');
   }
}

答案 3 :(得分:0)

这是100%有效的解决方案,因为我曾经使用Testdome进行的一次采访中希望对任何人有帮助。 但是,请不要仅仅为您的潜在雇主复制和粘贴,请花费更多的时间来理解问题和解决方案,问题看起来很困难,但答案很简单

{
    public function layEgg();
}

class Chicken implements Bird
{
    public $egg = null;
    public $eggnum;
    public function layEgg() {
        $this->egg = new Egg(self::class);
        echo "This is the egg";
        return $this->egg;
    }
}

class Egg
{
    public $birdType;
    protected $is_hatched = false;
    public function __construct($birdType)
    {
        $this->birdType = $birdType;
    }

    public function hatch()
    {
        if ($this->is_hatched){
            // throws
            throw new Exception('lay egg for 2nd time');
        }
        $bird_type = $this->birdType;
        $bird = new $bird_type();
        if (!($bird instanceof Bird)){
            // throws
        }

        $this->is_hatched = true;
        echo "I have hatched";
        return $bird;
    }
}