编程语言
首页 > 编程语言> > PHP 接口 interface 和实现 implement

PHP 接口 interface 和实现 implement

作者:互联网

interface User
{
    function getGender();
    function getLanguage();
}

class Male implements User
{
    function getGender()
    {
        return "Male";
    }

    function getLanguage()
    {
        return "Chinese";
    }
}

class Female implements User
{
    function getGender()
    {
        return "Female";
    }

    function getLanguage()
    {
        return "English";
    }
}

class Body
{
    function getUser(User $user)
    {
        echo "I am ".$user->getGender().",and i speak ".$user->getLanguage();
    }
}
$body = new Body();
$body->getUser(new Male);
$body->getUser(new Female);

输出结果:I am Male,and i speak ChineseI am Female,and i speak English

标签:function,Male,return,getGender,User,interface,implement,PHP
来源: https://www.cnblogs.com/weibofang/p/13806777.html