首页 > 代码库 > 继承性

继承性

   继承性是指建立一个新的派生类,从一个先前定义的类中继承数据和函数,而且可以重新定义或加进新数据和函数。说得简单点,继承性就是通过子类对已存在的父类进行功能扩展。


<?php

class person{

var $name;

var $sex;

var $age;


function __construct($a="",$b="",$c=""){

$this->name=$a;

$this->sex=$b;

$this->age=$c;

}


function say(){

echo "我的名字:".$this->name.";性别:".$this->sex.";年龄:".$this->age."。<br/>";

}


function run(){

echo $this->name."在走路<br/>";

}

}

//声明一个student类,使用extends关键字扩展(继承)person类

class student extends person{

var $school;

function study(){

echo $this->name."正在".$this->school."学习<br/>";

}

}

//再声明一个teacher类,使用extends关键字扩展(继承)student类

class teacher extends student{

var $wage;


function teaching(){

echo $this->name."正在".$this->school."教学;每月工资:".$this->wage."。<br/>";

}

}

$student1=new student("王五","女","20");

$teacher1=new teacher("张三","男","40");


$student1->school="二中";

$teacher1->school="一中";

$teacher1->wage="5000";


$student1->say();

$teacher1->say();

$student1->study();

$teacher1->teaching();

?>


代码运行结果如下:

技术分享

本文出自 “12145704” 博客,转载请与作者联系!

继承性