評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 1,平均評等: 5]
再來介紹 物件導向中 靜態屬性與方法的使用及特性,它是屬於整個類而非實體所以不用生成就可以使用。
class computer {
public static $num = 0;
public static function add (){
return self::$num+=1;
}
}
//
echo computer::$num.'<br>';//初始值
echo computer::add().'<br>';//第一次呼叫 顯示1
echo computer::add().'<br>';//第二次呼叫 顯示2
echo computer::add().'<br>';//第三次呼叫 顯示3
class b extends computer {
public static function add (){ //覆寫父方法
return self::$num+=2;//改加2
}
}
echo b::$num.'<br>';//值變 已經變3
echo b::add().'<br>';//子類別已覆寫父的方法 所以是加2 不是加1 顯示5
//靜態屬性與方法 使用時不用生成物件就可以直接使用 類別名稱::靜態屬性 類別名稱靜態方法()
//靜態屬性與方法 在類別中需使用 self or static 不可使用 this
//顯示結果
/*
1
2
3
3
5
*/
評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 1,平均評等: 5]
3M 日本原裝進口 速效型 防水噴霧 170ml 衣物 鞋子 皮革 透氣 防水 團購
好滋味御品/麻油猴頭菇
水壺背袋 水壺網袋 水瓶袋 多色可選 戶外郊遊 登山野餐 可放手機 水壺背帶
現貨超低價 小新玩偶 蠟筆小新 動感新生活係列 潮玩 小新公仔