1

我不知道为什么会出现此错误:传递给 App\src\Manager::manage() 的参数 1 必须是 App\src\ManageableInterface 的实例,给出的 App\src\Worker 的实例。我编码到接口 ManageableInterface 而不是具体类,但它不起作用。这是代码:

ManageableInterface.php

<?php namespace App\src\interfaces;

interface ManageableInterface
{
  public function manage();
}

SleepableInterface.php

<?php namespace App\src\interfaces;

interface SleepableInterface
{
  public function sleep();
}

工作界面.php

<?php namespace App\src\interfaces;

interface WorkableInterface
{
 public function work();
}

工人.php

<?php namespace App\src;

use App\src\interfaces\{WorkableInterface, SleepableInterface, ManageableInterface};

class Worker implements WorkableInterface, SleepableInterface, ManageableInterface
{
  public function work()
  {
    var_dump('Worker is working.');
  }


  public function sleep()
  {
    var_dump('Worker is sleeping.');
  }


  public function manage()
  {
    $this->work();
    $this->sleep();
  }

}

管理器.php

<?php namespace App\src;

class Manager
{
  public function manage(ManageableInterface $worker)
  {
    $worker->manage();
  }
 }

索引.php

<?php require 'vendor/autoload.php';

use App\src\{Manager, Worker};

$worker = new Worker();

$manager = new Manager();
$manager->manage($worker);
4

0 回答 0