評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 0,平均評等: 0]
上一篇介紹了 魔術方法 __call ,除了當防呆之外還可以拿來實現 overloading 多載,先簡單介紹一下多載是什麼,就是同一個方法 會因為所載入的參數的不同而去做不一樣的動作。
//__call 呼叫到不存在的方法後 會觸發到此方法
class People {
private $height;//身高
private $weight;//體重
private $sex;//姓別 要改成 保護 子類別才可以變更它的值
function __call($fun, $args) {
$count=count($args);
switch($fun) {
case 'set':
switch($count){
case 3:
if (is_int($args[0]) && is_int($args[1]) && is_string($args[2])) { // 數字,數字,字串 參數的格式
$this->height=$args[0];
$this->weight=$args[1];
$this->sex =$args[2];
}break;
case 2:
if (is_int($args[0]) && is_int($args[1])) { // 數字,數字 參數的格式
$this->height=$args[0];
$this->weight=$args[1];
}break;
case 1:
if (is_int($args[0]) ) { // 數字 參數的格式
$this->height=$args[0];
}break;
default:
$this->height=0;
$this->weight=0;
$this->sex ='male';
break;
}
case 'get'://取得三個參數
return '<br>jeff 的身高是:'.$this->height.
'<br>jeff 的體重是:'.$this->weight.
'<br>jeff 的姓別是:'.$this->sex.'<br>';
default:
echo '找不到'.$fun.'這個方法' ;
}
}
}
$jeff = new People();
echo '<br>帶三個參數<br>';
$jeff->set(170,65,'male');
echo $jeff->get();
echo '<br>帶兩個參數<br>';
$jeff->set(180,70);//只變更 身高 體重
echo $jeff->get();
echo '<br>帶一個參數<br>';
$jeff->set(190);//只變更 身高
echo $jeff->get();
echo '<br>第一個參數非數字時<br>';
$jeff->set('abc',99,'female');//這裡的值就不會寫進去
echo $jeff->get();//讀取時會讀到最後一次異動的值
echo '<br>其它非 get set 之外的方法<br>';
$jeff->gcc();
//顯示結果
/*
帶三個參數
jeff 的身高是:170
jeff 的體重是:65
jeff 的姓別是:male
帶兩個參數
jeff 的身高是:180
jeff 的體重是:70
jeff 的姓別是:male
帶一個參數
jeff 的身高是:190
jeff 的體重是:70
jeff 的姓別是:male
第一個參數非數字時
jeff 的身高是:190
jeff 的體重是:70
jeff 的姓別是:male
其它非 get set 之外的方法
找不到gcc這個方法
*/
評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 0,平均評等: 0]
水壺背袋 水壺網袋 水瓶袋 多色可選 戶外郊遊 登山野餐 可放手機 水壺背帶
垃圾桶 家用 客廳 臥室 辦公室 衛生間 大容量 高顏值 創意 收納 垃圾
3M 日本原裝進口 速效型 防水噴霧 170ml 衣物 鞋子 皮革 透氣 防水 團購
平衡豬 獨輪車小豬 平衡玩具 不倒翁平衡豬