子类是否自动实现父类的接口

mav*_*ili 1 php oop

这可能是一个基本的OOP问题,但我不确定答案,并且在Google搜索中找不到有用的东西(当然是第一页)

如果我有一个类是另一个实现接口的类的子类,那么我的子类是否会自动成为接口的实现,或者我必须明确地说它是什么?所以

interface HtmlElementInterface {
    public function getName();
}

abstract class HtmlElement implements HtmlElementInterface {
    protected $_name;

    public function __construct($name) {
        $this->_name = $name;
    }
    public function getName() {
        return $this->_name;
    }
    abstract public function __toString();
}

class TextInput extends HtmlElement {
    public function __toString() {
        return "<input type='input' name='{$this->_name}' id='{$this->_name}' />\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

在上述情况下,我没有告诉TextInputimplement HtmlElementInterface,难道不是吗?

Jua*_*des 6

不,TextInputHTMLElement这是一个HtmlElementInterface

  • @mavili听起来很公平 (3认同)
  • +1谢谢!@anolsi也在你准确的时间回答了,他的分数较少,所以我认为他们的答案是:) (2认同)

ano*_*lsi 5

不,你没有.这隐含着一种HtmlElementInterface实现.