本文实例讲述了PHP面向对象程序设计OOP继承用法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<?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();
?>
|
结果:
1
2
3
|
kaifu在说话
kaifu正在edu学习
kaifu正在edu教学,每月工资为4000
|
希望本文所述对大家PHP程序设计有所帮助。