PHP類繼承

2018-02-22 16:40 更新

PHP教程 - PHP類繼承

PHP類繼承

使用繼承,我們可以創(chuàng)建基于父類的子類。

子類繼承所有屬性和方法,也可以添加其他屬性和方法。

要?jiǎng)?chuàng)建基于父類的子類,請(qǐng)使用extends關(guān)鍵字,如下所示:

<?PHP
class Shape { 
   // (General Shape properties and methods here) 
} 
              
class Circle extends Shape { 
   // (Circle-specific properties and methods here) 
}     
?>

為了擴(kuò)展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é)果。



實(shí)施例2

下面的代碼顯示了如何調(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é)果。

PHP覆蓋方法

創(chuàng)建方法與中的相應(yīng)方法不同的子類父類通過(guò)重寫(xiě)父類“s"方法在子類中。

為此,只需在子類中創(chuàng)建一個(gè)具有相同名稱的方法。 然后,當(dāng)為子類的對(duì)象調(diào)用該方法名時(shí),運(yùn)行子類的方法而不是父類的方法:

<?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允許我們重新定義子類中的方法。這通過(guò)重新定義方法來(lái)完成里面的子類:

<?PHP
class Dog {
    public function bark() {
       print "Dog";
    }
}

class Puppy extends Dog {
   public function bark() {
      print "Puppy";
   }
}
?>

保留父類的功能

要調(diào)用重寫(xiě)的方法,在方法名之前寫(xiě)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é)果。

PHP最終類和方法

鎖定一個(gè)類,使它不能被繼承。或者鎖定類中的一個(gè)或多個(gè)方法,以便他們可以不在子類中覆蓋。

通過(guò)這樣做,你知道你的類或類中的方法將始終以完全相同的方式表現(xiàn)。

您可以在類或方法定義之前添加關(guān)鍵字final以鎖定該類或方法。

這里的如何創(chuàng)建一個(gè)final類:

final class MyClass { 

} 

這里是你如何做最終的方法:

class ParentClass { 
   public final function myMethod() { 
            echo "A method"; 
   } 
} 
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)