index.php:
<span style="font-size:24px;"><!DOCTYPE html>
<!--类的继承与方法重写-->
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
require_once 'Man.php';
$m= new Man("zhh",20);
//调用父类的方法
$m->sayHello();
?>
</body>
</html>
</span>
people.php
<span style="font-size:24px;"><?php
//构造方法:
/**
* Created by PhpStorm.
* User: zsgjs038
* Date: 2015/9/1
* Time: 13:47
*/
class People
{
/**
* @param $name 名字
* @param $age 年龄
* @param $sex 性别
*/
// 成员变量
private $name;
private $age;
private $sex;
// 上面的注释,在调用构造方法是会提示
// 构造方法名称都一样所以,只能写一个
public function __construct($name, $age, $sex)
{
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
public function getName()
{
return $this->name;
}
public function getAge()
{
return $this->age;
}
public function getSex(){
return $this->sex;
}
public function sayHello(){
print_r("fu lei de fang fa");
}
}</span>
Man.php
<span style="font-size:24px;"><?php
/**
* Created by PhpStorm.
* User: zsgjs038
* Date: 2015/9/1
* Time: 14:57
*/
require_once 'People.php';
class Man extends People{
public function __construct($name,$age){
// 调用父类的构造方法
parent::__construct($name,$age,"男");
}
// 重写父类中的方法(方法名,参数完全一样,其他没有任何标记)
public function sayHello(){
print_r("zi lei chong xie hou de fang fa");
echo '<br/>';
// 调用父类中的sayHello方法
parent::sayHello();
}
} </span>
源码下载:
http://download.csdn.net/detail/zhaihaohao1/9079123