PHP物件導向程式設計影片資料分享

巴扎黑
發布: 2023-03-15 16:22:01
原創
1367 人瀏覽過

在物件導向的程式設計(英文:Object-oriented programming,縮寫:OOP)中,物件是一個由資訊及對資訊進行處理的描述所組成的整體,是對現實世界的抽象。

在現實世界裡我們所面對的事情都是對象,如電腦、電視、腳踏車等。

物件的主要三個特性:

物件的行為:可以對 物件施加那些操作,開燈,關燈就是行為。

物件的形態:當施加那些方法是物件如何回應,顏色,尺寸,外型。

物件的表示:物件的表示就相當於身分證,具體區分在相同的行為與狀態下有什麼不同。

本課程透過講述物件導向的基本概念以及相關的案例實踐,讓小夥伴們對物件導向有一個基本的認識,能夠掌握把實際問題抽象化成為類別物件用以解決實際問題的方法,掌握物件導向的最重要的核心能力。

PHP物件導向程式設計影片資料分享

影片播放位址:http://www.php.cn/course/329.html

##本影片困難:

1. __construct: 

     內建建構函數,在物件建立時會自動呼叫。請參閱下列程式碼:

<? php
classConstructTest {
    private $arg1;
    private $arg2;
    public function __construct($arg1, $arg2) {
        $this->arg1 = $arg1;
        $this->arg2 = $arg2;
        print "__construct is called...\n";
    }
    public function printAttributes() {
        print &#39;$arg1 = &#39; . $this->arg1 . &#39; $arg2 = &#39; . $this->arg2 . "\n";
    }
}
$testObject = new ConstructTest("arg1", "arg2");
$testObject->printAttributes();
登入後複製

     運行結果如下:

Stephens-Air:Desktop$ php Test.php

__construct is called...
$arg1 = arg1 $arg2 = arg2

2. parent: 

     用於在子類別中直接呼叫父類別中的方法,功能等同於Java中的super。

<? php
classBaseClass {
    protected $arg1;
    protected $arg2;
    function __construct($arg1, $arg2) {
        $this->arg1 = $arg1;
        $this->arg2 = $arg2;
        print "__construct is called...\n";
    }
    function getAttributes() {
        return &#39;$arg1 = &#39; . $this->arg1 . &#39; $arg2 = &#39; . $this->arg2;
    }
}
class SubClass extends BaseClass {
    protected $arg3;
    function __construct($baseArg1, $baseArg2, $subArg3) {
        parent::__construct($baseArg1, $baseArg2);
        $this->arg3 = $subArg3;
    }
    function getAttributes() {
        return parent::getAttributes() . &#39; $arg3 = &#39; . $this->arg3;
    }
}
$testObject = new SubClass("arg1", "arg2", "arg3");
print $testObject->getAttributes() . "\n";
登入後複製

     運行結果如下:

Stephens-Air:Desktop$ php Test.php

__construct is called...
$arg1 = arg1 $arg2 = arg2 $arg3 = arg3

以上是PHP物件導向程式設計影片資料分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!