首頁  >  文章  >  後端開發  >  PHP物件導向程式設計OOP繼承用法入門範例

PHP物件導向程式設計OOP繼承用法入門範例

高洛峰
高洛峰原創
2017-01-06 13:51:131125瀏覽

本文實例講述了PHP物件導向程式設計OOP繼承用法。分享給大家供大家參考,具體如下:

<?php
class Person {
  var $name;//protected
  var $sex;
  var $age;
  function __construct($name = "", $sex = "男", $age = 22) {
    $this->name = $name;
    $this->sex = $sex;
    $this->age = $age;
  }
  function say() {
    echo $this->name . "在说话<br/>";
  }
  function run() {
    echo "在走路·<br/>";
  }
}
class Student extends Person {
  var $school;
  function __construct($name = "", $sex = "男", $age = 22,$school="") {
    parent::__construct($name,$sex,$age);
    $this->school = $school;
  }
  function study() {
    echo $this->name."正在".$this->school."学习<br/>";
  }
}
class Teacher extends Student {
  var $wage;
  function teaching() {
    echo $this->name."正在".$this->school."教学,每月工资为".$this->wage."<br/>";
  }
}
$teacher1 = new Teacher("kaifu","男",22);
$teacher1->school = "edu";
$teacher1->wage = 4000;
$teacher1->say();
$teacher1->study();
$teacher1->teaching();
?>

結果:

kaifu在说话
kaifu正在edu学习
kaifu正在edu教学,每月工资为4000

希望本文所述對大家PHP程式設計有所幫助。

更多PHP物件導向程式設計OOP繼承用法入門範例相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn