php中extends是什么意思,在php中extends与implements的区别

PHP

类是单继承,也就是不支持多继承,当一个类需要多个类的功能时,继承就无能为力了,为此 PHP 引入了类的接口技术。

接口的使用使用implements关键字,而对抽象类使用的是extends继承关键字。

在接口中只能定义常量和方法,不能实现方法,const定义常量,functionUser();不能使用pubilc

$a ="a"与 pubilc static$a

="a";

//抽象类

abstract class Father {

function name() {

echo "name...

";

}

abstract function name2();

public $n="n1";

public static $n2="n2";

const n3="n3";

}

class Data extends Father {

function name2() {

echo "name2 of Data...

";

}

}

$v=new Data();

echo $v->var1."

";

echo Father::$n2."

";

echo Father::n3."

";

//实现接口

interface User{

function getDiscount();

function getUserType();

}

//VIP用户 接口实现

class VipUser implements User{

private $discount = 0.5;

function getDiscount() {

return $this->discount;

}

function getUserType() {

return "VIP用户";

}

}

class Goods{

//public $price="45";        此处接口定义中不能包含成员变量

//public static $price="45"; 此处接口定义中不能包含静态变量

//

const price = 45;

public $vc;

//定义 User 接口类型参数,这时并不知道是什么用户

function run(User $vc){

$this->vc = $vc;

$discount = $this->vc->getDiscount();

$usertype = $this->vc->getUserType();

echo $usertype."商品价格:".self::price*$discount;

}

}

$display = new Goods();

$display ->run(new VipUser); //可以是更多其他用户类型

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值