php的继承代码,php继承用法基础代码教程

这篇文章主要介绍了PHP面向对象程序设计OOP继承用法,结合简单实例形式分析了php类的定义与继承使用方法,需要的朋友可以参考下

本文实例讲述了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 . "在说话
";

}

function run() {

echo "在走路·
";

}

}

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."学习
";

}

}

class Teacher extends Student {

var $wage;

function teaching() {

echo $this->name."正在".$this->school."教学,每月工资为".$this->wage."
";

}

}

$teacher1 = new Teacher("kaifu","男",22);

$teacher1->school = "edu";

$teacher1->wage = 4000;

$teacher1->say();

$teacher1->study();

$teacher1->teaching();

?>

结果:

kaifu在说话

kaifu正在edu学习

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值