課程
/后端開發
/PHP
/PHP面向對象編程
parent
self
static
2014-09-29
源自:PHP面向對象編程 4-5
正在回答
parent用于調用父類的靜態成員方法
self與static都可以用于訪問類自身定義的靜態成員方法,
<?phpdate_default_timezone_set("PRC");/**1 靜態屬性用于保存類的共有數據2 靜態方法里面只能訪問靜態屬性3 靜態成員不需要實例化對象就可以訪問4 類的內部可以通過self或者static關鍵字訪問自身靜態成員5 可以通過parent關鍵字訪問父類的靜態成員6可以通過類的名稱在定義外部訪問靜態成員*/class Human{?? ?public $name;?? ?protected $height; ?? ?public $weight;?? ?private $isHungry=true;?? ?public static $sValue = "Static value in Human class.";?? ?public function eat($food){?? ??? ?echo $this->name."'s eating".$food."\n";?? ? }}class Nbaplayer extends Human {?? ??? ??? ??? ?public $team = "bull";?? ??? ?public $playerNumber = "23";?? ??? ?private $age = "40";//靜態屬性定義時候在訪問關鍵字后面添加static 關鍵字即可?? ?public static $president = "David Stern";?? ?//靜態方法定義?? ?public static function changepresident($newprsdt){?? ?//在類中定義使用靜態成員時候,用self關鍵字后跟著屬性名?? ?//注意,在訪問靜態成員的時候,::后面需要跟符號$?? ??? ?self::$president = $newprsdt; //用static也可以?? ??? ?//使用parent關鍵字就能訪問父類的靜態成員?? ??? ?echo parent::$sValue."\n";?? ??? ??? ?}?? ??? ??? ?function __construct($name,$height,$weigh,$team,$playerNumber){?? ??? ??? ?echo "In Nbaplayer constructor\n";?? ??? ??? ?$this->name = $name; ?? ??? ??? ?$this->height = $height;?? ??? ??? ?$this->team = $team;?? ??? ??? ?$this->playerNumber = $playerNumber;?? ??? ?}? ? ?? ?function __destruct(){? ?? ??? ?echo "Destroying".$this->name."\n";? ?? ??? ?}?? ??? ?public function run(){??? //定義方法?? ??? ??? ?echo "running\n";?? ??? ?}?? ??? ?public function jump(){?? ??? ??? ?echo "jumpping\n";?? ??? ?}?? ??? ?public function dribble(){?? ??? ??? ?echo "dribbing\n";?? ??? ?}?? ??? ?public function dunk(){?? ??? ??? ?echo "dunking\n";?? ??? ?}?? ??? ?public function pass(){?? ??? ??? ?echo "passing\n";?? ??? ?}?? ??? ?public function getAge(){?? ??? ??? ?echo $this->name.".'s age is".($this->age - 2 )."\n";?? ??? ?}?? ?}?? ??? ??? ??? ??? ??? ?//類的實例化為對象時使用關鍵字new,new之后緊跟類名和一對括號?? ?? ?//在類定義外部訪問靜態屬性,我們可以用類名加::屬性名來調用靜態成員? ??? ??? echo Nbaplayer::$president . "before change\n";?? Nbaplayer::changepresident("Adam Silver");???? echo Nbaplayer::$president ."\n";???? echo Human::$sValue."\n";?? ??? ???? ?? //所有的對象共用一個屬性??? ?? ??>
舉報
從容應對面試官的知識寶典,帶你握面向對象的最重要的核心能力
2 回答
1 回答
3 回答
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-08-11
parent用于調用父類的靜態成員方法
self與static都可以用于訪問類自身定義的靜態成員方法,
<?php
date_default_timezone_set("PRC");
/**
1 靜態屬性用于保存類的共有數據
2 靜態方法里面只能訪問靜態屬性
3 靜態成員不需要實例化對象就可以訪問
4 類的內部可以通過self或者static關鍵字訪問自身靜態成員
5 可以通過parent關鍵字訪問父類的靜態成員
6可以通過類的名稱在定義外部訪問靜態成員
*/
class Human{
?? ?public $name;
?? ?protected $height;
?? ?public $weight;
?? ?private $isHungry=true;
?? ?public static $sValue = "Static value in Human class.";
?? ?public function eat($food){
?? ??? ?echo $this->name."'s eating".$food."\n";
?? ? }
}
class Nbaplayer extends Human {
?? ??? ?
?? ??? ?public $team = "bull";
?? ??? ?public $playerNumber = "23";
?? ??? ?private $age = "40";
//靜態屬性定義時候在訪問關鍵字后面添加static 關鍵字即可
?? ?public static $president = "David Stern";
?? ?//靜態方法定義
?? ?public static function changepresident($newprsdt){
?? ?//在類中定義使用靜態成員時候,用self關鍵字后跟著屬性名
?? ?//注意,在訪問靜態成員的時候,::后面需要跟符號$
?? ??? ?self::$president = $newprsdt; //用static也可以
?? ??? ?//使用parent關鍵字就能訪問父類的靜態成員
?? ??? ?echo parent::$sValue."\n";
?? ??? ?
?? ?}
?? ??? ?
?? ?function __construct($name,$height,$weigh,$team,$playerNumber){
?? ??? ??? ?echo "In Nbaplayer constructor\n";
?? ??? ??? ?$this->name = $name;
?? ??? ??? ?$this->height = $height;
?? ??? ??? ?$this->team = $team;
?? ??? ??? ?$this->playerNumber = $playerNumber;
?? ??? ?}
?
? ?? ?function __destruct(){
? ?? ??? ?echo "Destroying".$this->name."\n";
? ?? ??? ?}
?? ??? ?public function run(){??? //定義方法
?? ??? ??? ?echo "running\n";
?? ??? ?}
?? ??? ?public function jump(){
?? ??? ??? ?echo "jumpping\n";
?? ??? ?}
?? ??? ?public function dribble(){
?? ??? ??? ?echo "dribbing\n";
?? ??? ?}
?? ??? ?public function dunk(){
?? ??? ??? ?echo "dunking\n";
?? ??? ?}
?? ??? ?public function pass(){
?? ??? ??? ?echo "passing\n";
?? ??? ?}
?? ??? ?public function getAge(){
?? ??? ??? ?echo $this->name.".'s age is".($this->age - 2 )."\n";
?? ??? ?}
?? ?}
?? ??? ??? ??? ?
?? ??? ?//類的實例化為對象時使用關鍵字new,new之后緊跟類名和一對括號
?? ?? ?
//在類定義外部訪問靜態屬性,我們可以用類名加::屬性名來調用靜態成員? ?
?? ?
?? echo Nbaplayer::$president . "before change\n";
?? Nbaplayer::changepresident("Adam Silver");
???? echo Nbaplayer::$president ."\n";
???? echo Human::$sValue."\n";
?? ?
?? ?
??? ?? //所有的對象共用一個屬性
??? ?? ?
?>