PHP 接口 interface 和实现 implement
作者:互联网
- 接口类使用关键字 interface 来定义,且类中所有方法都是抽象方法,修饰符为 public,无声明变量
- 类通过使用关键字 implement 来实现 interface 中的方法
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