3

由于我是 C#-/.NET-guy,我习惯于显式接口实现- 就像这样:

public interface IBar
{
    bool Bacon();
}

public class Foo : IBar
{
    bool IBar.Bacon() {}
}

问题:
这可以在 php 中完成吗?

编辑:
为了澄清,这是隐含的(而我想要的以及上面示例中的内容是明确的):

public class Foo : IBar
{
    bool Bacon() {}
}
4

2 回答 2

6

PHP 支持接口,所以可以: http: //php.net/manual/en/language.oop5.interfaces.php

PHP 不区分隐式和显式实现。

于 2012-04-11T09:15:42.667 回答
-1

鉴于下面的代码,我非常坚持与代码一起执行的任务:

当一个 ReptileEgg 孵化时,一个新的爬行动物将与产卵的物种相同。

当 Reptile 是一个接口时,如何在 ->hatch 内部创建一个新的爬虫?这是一个家庭作业,我没能及时弄清楚。它会让我发疯,直到我回去弄清楚。

<?php
interface Reptile
{
    public function layEgg() : ReptileEgg;
}

class FireDragon implements Reptile
{
    public function layEgg() : ReptileEgg {
        
    }
}

class ReptileEgg
{
    public function __construct(string $reptileType)
    {
        
    }
    
    public function hatch() : ? Reptile
    {
        return null;
    }
}
于 2021-06-01T21:56:23.460 回答