使用繼承,我們可以創(chuàng)建基于父類的子類。
子類繼承所有屬性和方法,也可以添加其他屬性和方法。
要創(chuàng)建基于父類的子類,請使用extends關(guān)鍵字,如下所示:
<?PHP class Shape { // (General Shape properties and methods here) } class Circle extends Shape { // (Circle-specific properties and methods here) } ?>
為了擴展Dog類,我們可以使用extends關(guān)鍵字。
<?PHP class Dog { public function bark() { print "Woof!\n"; } } class Puppy extends Dog { } ?>
下面的代碼顯示了如何用self調(diào)用當(dāng)前類。
<?php class Animal { public $blood; public $name; public function __construct($blood, $name=NULL) { $this->blood = $blood; if($name) { $this->name = $name; } } } class Mammal extends Animal { public $furColor; public $legs; function __construct($furColor, $legs, $name=NULL) { parent::__construct("warm", $name); $this->furColor = $furColor; $this->legs = $legs; } } class Dog extends Mammal { function __construct($furColor, $name) { parent::__construct($furColor, 4, $name); self::bark(); } function bark() { print("$this->name says "woof!""); } } $d = new Dog("Black and Tan", "Angus"); ?>
上面的代碼生成以下結(jié)果。
下面的代碼顯示了如何調(diào)用父構(gòu)造函數(shù)。
<?php class Calculator { protected $_val1, $_val2; public function __construct( $val1, $val2 ) { $this->_val1 = $val1; $this->_val2 = $val2; } public function add() { return $this->_val1 + $this->_val2; } public function subtract() { return $this->_val1 - $this->_val2; } public function multiply() { return $this->_val1 * $this->_val2; } public function divide() { return $this->_val1 / $this->_val2; } } class CalcAdvanced extends Calculator { private static $_allowedFunctions = array( "pow" => 2, "sqrt" => 1, "exp" => 1 ); public function __construct( $val1, $val2=null ) { parent::__construct( $val1, $val2 ); } public function __call( $methodName, $arguments ) { if ( in_array( $methodName, array_keys( CalcAdvanced::$_allowedFunctions ) ) ) { $functionArguments = array( $this->_val1 ); if ( CalcAdvanced::$_allowedFunctions[$methodName] == 2 ) array_push( $functionArguments, $this->_val2 ); return call_user_func_array( $methodName, $functionArguments ); } else { die ( "<p>Method "CalcAdvanced::$methodName" doesn"t exist</p>" ); } } } $ca = new CalcAdvanced( 3, 4 ); echo "<p>3 + 4 = " . $ca->add() . "</p>"; echo "<p>3 - 4 = " . $ca->subtract() . "</p>"; echo "<p>3 * 4 = " . $ca->multiply() . "</p>"; echo "<p>3 / 4 = " . $ca->divide() . "</p>"; echo "<p>pow( 3, 4 ) = " . $ca->pow() . "</p>"; echo "<p>sqrt( 3 ) = " . $ca->sqrt() . "</p>"; echo "<p>exp( 3 ) = " . $ca->exp() . "</p>"; ?>
上面的代碼生成以下結(jié)果。
創(chuàng)建方法與中的相應(yīng)方法不同的子類父類通過重寫父類“s"方法在子類中。
為此,只需在子類中創(chuàng)建一個具有相同名稱的方法。 然后,當(dāng)為子類的對象調(diào)用該方法名時,運行子類的方法而不是父類的方法:
<?PHP class ParentClass { public function someMethod() { // (do stuff here) } } class ChildClass extends ParentClass { public function someMethod() { // This method is called instead for ChildClass objects } } $parentObj = new ParentClass; $parentObj->someMethod(); // Calls ParentClass::someMethod() $childObj = new ChildClass; $childObj->someMethod(); // Calls ChildClass::someMethod() ?>
PHP允許我們重新定義子類中的方法。這通過重新定義方法來完成里面的子類:
<?PHP class Dog { public function bark() { print "Dog"; } } class Puppy extends Dog { public function bark() { print "Puppy"; } } ?>
要調(diào)用重寫的方法,在方法名之前寫parent :::
parent::someMethod();
<?php class Fruit { public function peel() { echo "peeling the fruit.\n"; } public function slice() { echo "slicing the fruit.\n"; } public function eat() { echo "eating the fruit. \n"; } public function consume() { $this-> peel(); $this-> slice(); $this-> eat(); } } class Banana extends Fruit { public function consume() { echo " < p > I"m breaking off a banana... < /p > "; parent::consume(); } } $apple = new Fruit; $apple->consume(); $banana = new Banana; $banana->consume(); ?>
上面的代碼生成以下結(jié)果。
鎖定一個類,使它不能被繼承?;蛘哝i定類中的一個或多個方法,以便他們可以不在子類中覆蓋。
通過這樣做,你知道你的類或類中的方法將始終以完全相同的方式表現(xiàn)。
您可以在類或方法定義之前添加關(guān)鍵字final以鎖定該類或方法。
這里的如何創(chuàng)建一個final類:
final class MyClass { }
這里是你如何做最終的方法:
class ParentClass { public final function myMethod() { echo "A method"; } }
更多建議: