enter3017sky / mentor-program-2nd-blog

MTR02 學習紀錄
1 stars 2 forks source link

[筆記][MTR02] Week5-2:物件導向程式設計 #24

Open enter3017sky opened 6 years ago

enter3017sky commented 6 years ago

MTR02 Week5-2:物件導向程式設計 MTR02 Week5-2(續):繼承與 static

物件導向設計(Object-Oriented Programming);

面向對象(中國翻譯)

為什麼要有物件導向?

// 計算機 範例一
function setResult() {
}

function appendResult() {
}

function inputNumber() {
}

function inputOperator() {
}

document.querySelector('.number').addEventListener('click', function(){
    inputNumber(document.querySelector('.number').innerText)
})

document.querySelector('.operator').addEventListener('click', function(){
    inputOperator(document.querySelector('.number').innerText)
})
// 學生的資訊 範例二
const students = []
const studentScore = []
const studentName = []

{
    name:'peter',
    score: 100,
    height: '190',
}

類別 Class VS 實體 Instance

<?php

class Dog {
    function hello() { //method 方法
        echo "I am dog";
    }
}
// 用 new 創建一隻狗
$dog = new Dog();

// doe 的 hello()方法
$dog->hello();

?>

延伸閱讀: 什麼是物件導向(2):Object, Class, Instance 翻譯名詞:object, instance 物件原型 | MDN

PHP 中的 Class

封裝 Encapsulation

<?php
class Dog {
    //首先把 hello() 設定成 public 的function ,用 $a->hello() 它
    public $name = ', name is default';
    public function hello() { 
        echo $this->getHelloSentence();
    }
    private function getHelloSentence(){
        return '<br> I am a dog' . $this->name; // 存取自己實體的東西用 this
    }
}

$a = new Dog();
$a->name = ', my name is Leo';
$a->hello(); // JS 裡面是 a.hello()
// result=> I am a dog, my name is Leo
$b = new Dog();
$b->name = ', my name is Boo';
$b->hello(); // 執行這個的時候 this 就是 b的名稱
?>
// result=> I am a dog, my name is Boo
<?php
class Dog {
    //首先把 hello() 設定成 public 的function ,用 $a->hello() 它
    private $name = ', name is default';
    public function hello() { 
        echo $this->getHelloSentence();
    }
    private function getHelloSentence(){
        return '<br> I am a dog' . $this->name; // 存取自己實體的東西用 this
    }

    // 譬如東西無意間被改到
    // 譬如 name 不能是髒話,用 public 不能限制

    // 可以做檢查 保證裡面的東西不會被變更到
    // setter 專門設定變數
    public function setName($name) {
        if($name === 'fuck') return; // 設定檢查
        $this->name = $name;
    }
    // getter 傳回變數
    public function getName() {
        return $this->name;
    }
}

$a = new Dog();
$a->setName(', my name is Leo');
$a->hello();
// result=> I am a dog, my name is Leo
$b = new Dog();
$b->setName(', my name is Boo');
$b->hello();
// result=> I am a dog, my name is Boo
?>
// 我要一個新的 request
let request = new XMLHttpRequest();
// 送到哪裡去
request.open('GET', url, true);
// 加一個 callback function
request.onload = () => {
    ...
}
// 最後把 request 送回去
request.send()
// ES6 class 語法,跟下面的一樣
/*
class Calculator {
    constructor() {
        this.text = ''
    }
    input(str) {
        this.text += str
    }
    getResult() {
        return eval(this.text)
    }
}
const calculator = new Calculator('name');
calculator.input(1)
calculator.input('+')
calculator.input(3)
const result = calculator.getResult()
console.log(result)
*/

// ES5
// constructor 建構子
class Calculator {
    constructor(){
        this.name = name
        this.text = '' 
    }
}

Calculator.prototype.input = function(str) {
    this.text += str
}

Calculator.prototype.getResult = function(){
    return eval(this.text)
}

const calculator = new Calculator('name');
calculator.input(1)
calculator.input('+')
calculator.input(3)
const result = calculator.getResult()
console.log(result)

Static 靜態方法 vs Method 方法

class MentorProgramAPI {
    static $version = 1;
    public function getTwitchResult(){
    }
}
$api = new MentorProgramAPI();
$api->getTwitchResult();
echo MentorProgramAPI::$version;
class MentorProgramAPI {
    public $version = 1;
    public function getTwitchResult(){
    }
}

$api = new MentorProgramAPI();
$api->getTwitchResult();
$api->version;
echo $api->version;

$api2 = new MentorProgramAPI();
$api2->getTwitchResult();
$api2->version;
echo $api2->version;

// 繼承 Inheritance
// public 公用的
// private 私用資料
// protected 被保護的
// instance 實例;例子

// 宣告這個說明書(Animal),New 一個 instance(Dog) 出來才可以用裡面這些 method。
//
class Animal {
    public $name = 'default';
    // protected $name = 'default';
    //初始化
    public function __construct($name) { //建構子
        $this->name = $name;
    }
    public function sayHello(){
        echo '動物 ' . $this->name;
    }
}

// extends 擴充;延伸。
// Dog 擴充 動物這個類別
class Dog extends Animal {
    public function run() {
        echo ' I am running';
    }
    public function sayHello(){ // override 複寫
        echo 'I am dog'. $this->name;
    }
    public function sayYoAndHello() {
        parent::sayHello();
        echo 'yo';
    }
    public static function test() {
        echo 'test' ;
    }
}
// Dog::test()

// 靜態方法:static。Class 本身也可以有 function ,這個好處是,這個東西本身不會被改變,然後是建立在 Dog 底下的 function

class MentorProgramAPI {
    static $version = 1;
    public function getTwitchResult(){
    }
}

$api = new MentorProgramAPI();
$api->getTwitchResult();
echo MentorProgramAPI::$version;

// $api2 = new MentorProgramAPI();
// $api2->getTwitchResult();
// $api2->version;
// echo $api2->version;

//$a = new Dog(' leo '); // 新增一隻狗
// $a->name;    // protected 也沒辦法用
// $a->sayYoAndHello();
// $a->sayHello();
// $a->run();

class Db {
    public function __construct($server, $user, $password, $database) {
        $this->sevverName = $s;
    $this->userName = $u;
    $this->init()
    }

    private function init() {
        $this->conn = myqdli_db_connect(....)
    }

    public function query($str) {
        $this->result = myqdli_query($this->conn, $str)
    }
}

mysqli_connect($conn)
myqdli_query($conn, $query)
myqdli_fetch_result($conn, $query) //顯式

$conn->connect()
$conn->query($query)
$conn->fetch_result($query)
// 建構子 constructor
class Animal {
    constructor(name) {
        this.name = name
    }
    hello() {
        console.log(this.name)
    }
}
const dog = new Animal('My dog')
dog.hello()

const cat = new Animal('My cat')
cat.hello()