自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PHP parent調(diào)用父類構(gòu)造函數(shù)

開發(fā) 后端
PHP程序員們在實(shí)際編寫中使用PHP parent來指向父類指針,并調(diào)用父類的構(gòu)造函數(shù),對父類進(jìn)行初始化。下面將給出具體的實(shí)現(xiàn)方法。

大家在學(xué)習(xí)PHP語言的時候,都會對與指針相關(guān)的內(nèi)容感到特別的頭疼。很難理解并不代表不用了解。下面我們就來看看PHP parent是如何指向父類指針的。#t#

我們知道PHP parent是指向父類的指針,一般我們使用parent來調(diào)用父類的構(gòu)造函數(shù)。

 

  1. < ?php  
  2. //基類  
  3. class Animal  
  4. {  
  5. //基類的屬性  
  6. public $name; //名字  
  7. //基類的構(gòu)造函數(shù)  
  8. public function __construct( $name )  
  9. {  
  10. $this->name = $name;  
  11. }  
  12. }  
  13. //派生類  
  14. class Person extends Animal   
  15. //Person類繼承了Animal類  
  16. {  
  17. public $personSex; //性別  
  18. public $personAge; //年齡  
  19. //繼承類的構(gòu)造函數(shù)  
  20. function __construct( $personSex, 
    $personAge )  
  21. {  
  22. parent::__construct( "heiyeluren" );
     //使用parent調(diào)用了父類的構(gòu)造函數(shù)  
  23. $this->personSex = $personSex;  
  24. $this->personAge = $personAge;  
  25. }  
  26. function printPerson()  
  27. {  
  28. print( $this->name. " is " .$this->
    personSex. ",this year " .$this->
    personAge );  
  29. }  
  30. }  
  31. //實(shí)例化Person對象  
  32. $personObject = new Person( "male", "21");  
  33. //執(zhí)行打印  
  34. $personObject->printPerson();   
  35. //輸出:heiyeluren is male,this year 21  
  36. ?>  

 

我們注意這么幾個細(xì)節(jié):成員屬性都是public的,特別是父類的,是為了供繼承類通過this來訪問。我們注意關(guān)鍵的地方,第25行:parent:: __construct( "heiyeluren" ),這時候我們就使用PHP parent來調(diào)用父類的構(gòu)造函數(shù)進(jìn)行對父類的初始化,因?yàn)楦割惖某蓡T都是public的,于是我們就能夠在繼承類中直接使用 this來調(diào)用。

 

責(zé)任編輯:曹凱 來源: 在線閱讀
相關(guān)推薦

2009-08-14 09:15:28

C#調(diào)用構(gòu)造函數(shù)

2009-11-18 12:31:19

PHP構(gòu)造函數(shù)

2009-11-16 16:59:03

PHP構(gòu)造函數(shù)

2009-11-23 14:44:22

PHP 5.0構(gòu)造函數(shù)

2009-09-18 13:40:40

繼承關(guān)系

2009-11-16 16:54:00

PHP構(gòu)造函數(shù)

2010-01-27 10:13:22

C++類對象

2009-12-11 10:42:00

Scala講座類定義構(gòu)造函數(shù)

2010-01-25 14:00:27

C++類

2009-12-08 14:00:11

PHP函數(shù)microt

2010-07-28 12:47:06

Flex組件

2009-08-13 18:36:36

C#繼承構(gòu)造函數(shù)

2010-01-20 18:06:06

C++虛基類

2023-12-07 07:41:15

JavaScript函數(shù)原型

2025-02-06 13:23:09

C++函數(shù)參數(shù)

2009-10-23 11:31:05

CLR Via C#調(diào)

2009-08-13 18:26:35

C#繼承構(gòu)造函數(shù)

2011-07-20 13:40:09

拷貝構(gòu)造函數(shù)

2021-12-09 10:56:50

函數(shù)C++原型

2009-12-08 17:01:01

PHP PEAR DB
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號