close

學程式的道路上.....我跌跌撞撞的過程

文章為我平時發生錯誤的解決方法

因為擔心下次碰到時會忘記

所以就將他筆記下來了

如果有大大發現我的敘述錯誤

或者有哪些更有效率的方法

也請大大們不吝嗇的提供指教

謝謝~


今天要寫介紹『繼承』的觀念

繼承的觀念可以把他想像成『爸爸』與『兒子』的關係

『兒子』會擁有『爸爸』的基因,也可以有自己的個性與興趣

所以『兒子』可以有『爸爸』的所有行為,又會另外衍生自己的行為出來

首先,我們先來建立第一個類別

demo.php

<?php
namespace APP;
class Father{
    protected $name;
    protected $age;

    public function __construct($new_name = 'Lee', $new_age = 45)
    {
        $this->name = $new_name;
        $this->age = $new_age;
    }
    public function getName()
    {
        return $this->name;
    }
    public function getAge()
    {
        return $this->age;
    }
}

現在我們已經建立了一個類別,『Father』就是這一個類別的名子

那先複習上一篇文章的內容,我該怎麼呼叫這一個類別呢?

可以參考下方的程式碼

<?php
namespace APP;
class Father{
    protected $name;
    protected $age;

    public function __construct($new_name = 'Lee', $new_age = 45)
    {
        $this->name = $new_name;
        $this->age = $new_age;
    }
    public function getName()
    {
        return $this->name;
    }
    public function getAge()
    {
        return $this->age;
    }
}

$father = new Father();
echo '父親的名字: ' . $father->getName() . ',父親的年齡是:' . $father->getAge();

//輸出結果 :

這邊,我宣告了一個新的物件$father

並且用$father物件執行Father類別底下的函式 getName() 與 getAge()

這樣就可以得到輸出結果啦~


現在已經建立了一個Father物件,也複習過上一篇文章的內容

接著要開始講解『繼承』的觀念啦

首先我們會在建立一個類別,就把這個類別取名成Son

如下方程式碼

<?php
namespace APP;
class Father{
    protected $name;
    protected $age;

    public function __construct($new_name = 'Lee', $new_age = 45)
    {
        $this->name = $new_name;
        $this->age = $new_age;
    }
    public function getName()
    {
        return $this->name;
    }
    public function getAge()
    {
        return $this->age;
    }
}

class Son extends Father{
    //這裡還沒打程式碼
}

這邊你應該注意到class Son extends Father這一行,你會發現多了extends Father

extends表示『繼承』的意思,所以要繼承『父類別』就必須加上extends

接著當然要跟電腦說要『繼承』誰囉~所以就會在extends後方加上類別名稱Father

那已經完成計成了,又該如何運作程式碼呢?

請看下方程式碼

<?php
namespace APP;
class Father{
    protected $name;
    protected $age;

    public function __construct($new_name = 'Lee', $new_age = 45)
    {
        $this->name = $new_name;
        $this->age = $new_age;
    }
    public function getName()
    {
        return $this->name;
    }
    public function getAge()
    {
        return $this->age;
    }
}

class Son extends Father{
    //這裡還沒打程式碼
}

$son = new Son('Neil', 16);
echo '兒子的名字:' . $son->getName() . ' 兒子的年齡是:' . $son->getAge();

//輸出結果:兒子的名字:Neil 兒子的年齡是:16

講解一下執行順序:

1.宣告了一個$son物件,兒這個物件帶入兩個值(字串Neil,數字:16)

這樣子程式碼就會自動去執行『父類別(Father)』的『建構子(construct)』

所以現在的 $name $age 就已經變成 Neil16 

然後再去用$son物件分別去執行『父類別(Father)』的函式 getName() 與 getAge()

這樣就可以得到輸出結果啦~


這樣就學會基本的繼承啦~接下來就要在『子類別』新增自己的行為

<?php
namespace APP;
class Father{
    protected $name;
    protected $age;

    public function __construct($new_name = 'Lee', $new_age = 45)
    {
        $this->name = $new_name;
        $this->age = $new_age;
    }
    public function getName()
    {
        return $this->name;
    }
    public function getAge()
    {
        return $this->age;
    }
}

class Son extends Father{
    protected $hobby;

    public function setHobby($myhobby)
    {
        $this->hobby = $myhobby;
    }
    public function getHobby()
    {
        return $this->hobby;
    }
}

現在新增了一個調查興趣的函式,函式名稱是setHobby()

並透過getHobby()這一個函式取得興趣

所以下方程式碼就是要開始執行啦~

如下方程式碼

<?php
namespace APP;
class Father{
    protected $name;
    protected $age;

    public function __construct($new_name = 'Lee', $new_age = 45)
    {
        $this->name = $new_name;
        $this->age = $new_age;
    }
    public function getName()
    {
        return $this->name;
    }
    public function getAge()
    {
        return $this->age;
    }
}

class Son extends Father{
    protected $hobby;

    public function setHobby($myhobby)
    {
        $this->hobby = $myhobby;
    }
    public function getHobby()
    {
        return $this->hobby;
    }
}

$son = new Son('Neil', 16);
echo '兒子的名字:' . $son->getName() . ' 兒子的年齡是:' . $son->getAge() . "\n";

//呼叫Son類別底下的函式 setHobby & getHobby 
//首先先設定興趣 
$son->setHobby('打網球'); 

//接著呼叫興趣 
echo '兒子的興趣是: ' . $son->getHobby();


//輸出結果:
兒子的名字:Neil 兒子的年齡是:16
兒子的興趣是: 打網球

這樣子我就可以得到結果啦

仔細看一下紅色文字,$son->setHobby('打網球'); 這一行是執行Son類別底下的setHobby()函式

『目的在輸入興趣的值』

然後我在執行echo '兒子的興趣是: ' . $son->getHobby();這一行,透過$son物件呼叫getHobby()函式

取得最後『興趣的值』


這樣就完成『繼承』的基本概念了

物件導向(一)

物件導向(二)就講解完畢啦~

下一篇文章物件導向(三)就要介紹『靜態』的筆記

arrow
arrow

    Neil 發表在 痞客邦 留言(0) 人氣()