2
<?php
//为了方便理解用了中文。
interface 男
{
}

interface 女
{
}

class 人
{
    public function 性别()
    {
        if($this instanceof 男)
        {
            return '男';
        }
        if($this instanceof 女)
        {
            return '女';
        }
        
        return '未知';
    }
}

class 李雷 extends 人 implements 男
{
}

class 韩梅梅 extends 人 implements 女
{
}

echo (new 李雷())->性别() . PHP_EOL;
echo (new 韩梅梅())->性别() . PHP_EOL;

空接口的作用是标识。给实现该接口的所有类打上标识,利用这个标识可以对这些类做统一的处理。


上官元恒
8.7k 声望1.9k 粉丝

吃好喝好