1. Home
  2. /
  3. Web technology
  4. /
  5. PHP
  6. /
  7. OOP
  8. /
  9. [ PHP ] –...

[ PHP ] – OOP 6 物件 繼承 extends

評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 0,平均評等: 0]

這篇介紹的是物件導向程式設計的最大重點,繼承 extends 也就是讓你少寫很多重覆的code的重點功能,廢話不多說直接看範例。

//extends 繼承 你可以想成遺傳 你爸爸有的一些特徵 在你的身上也會看到,但又沒有完全一樣(又不是複製人), 但你會的又比你的爸爸多
class People {
	private $height;//身高
	private $weight;//體重
	private $sex;//姓別
	function set_height($height1) { $this->height = $height1;}
	function set_weight($weight1) {$this->weight = $weight1;}
	function set_sex($sex1) {$this->sex = $sex1;}
	function get_height() {return $this->height;}
	function get_weight() {return $this->weight;}
	function get_sex() {return $this->sex;}
}
class People2 extends People { //People2 繼承了 people 內所有的屬性跟方法
	private $action;//動作
	function set_action($action1) {$this->action = $action1;}
	function get_action() {return $this->action;}
}
$jeff = new People2();
$jeff->set_height(170);
$jeff->set_weight(65);
$jeff->set_sex('male');
$jeff->set_action('sit');
echo 'jeff 的身高是:';
echo $jeff->get_height();
echo '<br>jeff 的體重是:';
echo $jeff->get_weight();
echo '<br>jeff 的姓別是:';
echo $jeff->get_sex();
echo '<br>jeff 的動作是:';
echo $jeff->get_action();
//顯示結果
/*
jeff 的身高是:170
jeff 的體重是:65
jeff 的姓別是:male
jeff 的動作是:sit
*/

評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 0,平均評等: 0]
!去下營經續永站本助幫能就下一點輕輕,話的您到助幫有章文的我果如 如果我的文章有幫助到您的話,輕輕點一下就能幫助本站永續經營下去!

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *