PHP物件導向特性 繼承

2021-08-04 05:39:22 字數 1376 閱讀 1475

1、繼承

定義:繼承是從乙個基類得到乙個或多個類的機制。

關鍵字:extends

注: php只支援單繼承,不支援方法過載。

2、例子

class

computer

} class

notecomputer

extends

computer

$notecomputer = new notecomputer();

echo

$notecomputer->_name;

$notecomputer->_run();

3、重寫

class

computer

} class

notecomputer

extends

computer

}$notecomputer = new notecomputer();

echo

$notecomputer->_name;

$notecomputer->_run();

4、protected

父類私有化時,無法被子類繼承,這個時候應該用受保護的(protected )修飾符。

class

computer

} class

notecomputer

extends

computer

}$notecomputer = new notecomputer();

$notecomputer->gettop();

5、重寫呼叫父類方法

子類已經覆蓋父類字段和方法,還想呼叫父類該怎麼辦?

class

computer

} class

notecomputer

extends

computer

}$notecomputer = new notecomputer();

echo

$notecomputer->_name;

$notecomputer->_run();

6、final關鍵字

//final 如果加在類前 表示不能被繼承

final

class

computer

class

notecomputer

extends

computer

class

computer

class

notecomputer

extends

computer

物件導向特性 繼承

1 子類繼承父類的方法和字段 class computer 子類,膝上型電腦類 class notecomputer extends computer 2 不需要父類的字段和方法,那麼可以採用重寫的方法覆蓋掉父類的方法。class computer 子類,膝上型電腦類 class notecompu...

PHP物件導向特性

1.類的屬性 成員變數 屬性宣告是由關鍵字 public,protected 或者 private 開頭,然後跟乙個普通的變數宣告來組成。其中public在物件中可以直接訪問,其他不可以。屬性宣告 該類子類 public protected private 2.static靜態屬性 宣告類屬性或方法...

物件導向特性 php

1.類的字段呼叫格式 公用字段 類內呼叫 this 欄位名 類外呼叫 物件名 欄位名 靜態 類內呼叫 self 欄位名 類外呼叫 類名 欄位名 常量 類內呼叫 self 欄位名 類外呼叫 類名 欄位名 class computer public function get key public sta...