php 类继承是什么意思,PHP类继承

PHP教程 - PHP类继承

PHP类继承

使用继承,我们可以创建基于父类的子类。

子类继承所有属性和方法,也可以添加其他属性和方法。

要创建基于父类的子类,请使用extends关键字,如下所示:

class Shape {

// (General Shape properties and methods here)

}

class Circle extends Shape {

// (Circle-specific properties and methods here)

}

?>

为了扩展Dog类,我们可以使用extends关键字。

class Dog {

public function bark() {

print "Woof!\n";

}

}

class Puppy extends Dog {

}

?>

例子

下面的代码显示了如何用self调用当前类。

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");

?>

上面的代码生成以下结果。

cc250af5c6a2b9eba64a65f2d59875cf.png

实施例2

下面的代码显示了如何调用父构造函数。

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 ( "

Method "CalcAdvanced::$methodName" doesn"t exist

" );

}

}

}

$ca = new CalcAdvanced( 3, 4 );

echo "

3 + 4 = " . $ca->add() . "

";

echo "

3 - 4 = " . $ca->subtract() . "

";

echo "

3 * 4 = " . $ca->multiply() . "

";

echo "

3 / 4 = " . $ca->divide() . "

";

echo "

pow( 3, 4 ) = " . $ca->pow() . "

";

echo "

sqrt( 3 ) = " . $ca->sqrt() . "

";

echo "

exp( 3 ) = " . $ca->exp() . "

";

?>

上面的代码生成以下结果。

309a1a501075c6f0f7bd6d88dcb04b52.png

PHP覆盖方法

创建方法与中的相应方法不同的子类父类通过重写父类“s"方法在子类中。

为此,只需在子类中创建一个具有相同名称的方法。 然后,当为子类的对象调用该方法名时,运行子类的方法而不是父类的方法:

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允许我们重新定义子类中的方法。这通过重新定义方法来完成里面的子类:

class Dog {

public function bark() {

print "Dog";

}

}

class Puppy extends Dog {

public function bark() {

print "Puppy";

}

}

?>

保留父类的功能

要调用重写的方法,在方法名之前写parent :::

parent::someMethod();

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();

?>

上面的代码生成以下结果。

dacb588992459a227330eea91e5141cc.png

PHP最终类和方法

锁定一个类,使它不能被继承。或者锁定类中的一个或多个方法,以便他们可以不在子类中覆盖。

通过这样做,你知道你的类或类中的方法将始终以完全相同的方式表现。

您可以在类或方法定义之前添加关键字final以锁定该类或方法。

这里的如何创建一个final类:

final class MyClass {

}

这里是你如何做最终的方法:

class ParentClass {

public final function myMethod() {

echo "A method";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值