您的位置:首页 > 编程语言 > PHP开发

PHP类与继承

2014-01-03 22:33 148 查看
<?php
class Person
{
private $name;
private $age;

function __construct($name,$age){
$this->name = $name;
$this->age = $age;
}

public function setName($name)
{
$this->name = $name;
}

public function getName()
{
return $this->name;
}

public function setAge($age)
{
$this->age = $age;
}

public function getAge()
{
return $this->age;
}

public function SayHello()
{
printf("<br/>name:%s,age:%d",$this->name,$this->age);
}
}

$person = new Person("zhangsan", 20);
$person->SayHello();

class Student extends  Person
{
private $work;
function __construct($name,$age,$work)
{
parent::__construct($name, $age);
$this->work = $work;
}

public function setWork($work)
{
$this->work = "学生";
}

public function getWork()
{
return $this->work;
}

public function Introduce()
{
printf("<br/>name:%s,age:%d,work:%s",$this->getName(),$this->getAge(),$this->getWork());
}
}

$student = new Student("wangwu", 18, "学生");
$student->SayHello();
$student->Introduce();
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: