日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

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

發(fā)布時(shí)間:2024/9/19 php 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php中extends是什么意思,在php中extends与implements的区别 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

PHP

類是單繼承,也就是不支持多繼承,當(dāng)一個(gè)類需要多個(gè)類的功能時(shí),繼承就無(wú)能為力了,為此 PHP 引入了類的接口技術(shù)。

接口的使用使用implements關(guān)鍵字,而對(duì)抽象類使用的是extends繼承關(guān)鍵字。

在接口中只能定義常量和方法,不能實(shí)現(xiàn)方法,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."

";

//實(shí)現(xiàn)接口

interface User{

function getDiscount();

function getUserType();

}

//VIP用戶 接口實(shí)現(xiàn)

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"; 此處接口定義中不能包含靜態(tài)變量

//

const price = 45;

public $vc;

//定義 User 接口類型參數(shù),這時(shí)并不知道是什么用戶

function run(User $vc){

$this->vc = $vc;

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

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

echo $usertype."商品價(jià)格:".self::price*$discount;

}

}

$display = new Goods();

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

?>

總結(jié)

以上是生活随笔為你收集整理的php中extends是什么意思,在php中extends与implements的区别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。