PHP类与继承

时间:2022-01-03 22:02:00
<?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();
?>