一步一步学习PHP(6) 面向对象

2019-04-10 00:33:52丽君


<?php
class Person
{
private $name;
private $age;
public function Person($name,$age)
{
$this->name=$name;
$this->age=$age;
}
public function SayHello()
{
echo("Hello,My name is ".$this->name.".I'm ".$this->age);
}
}
class Student extends Person
{
private $score;
public function Student($name,$age,$score)
{
$this->Person($name,$age);
$this->score=$score;
}
public function Introduce()
{
parent::SayHello();
echo(".In this exam,I got ".$this->score);
}
}

$s=new Student("kym",22,120);
$s->Introduce();
?>

5. 析构函数

析构函数和C#和C++中不同,在PHP中,析构函数的名称是__destructor()。

class Student extends Person
{
private $score;
public function Student($name,$age,$score)
{
$this->Person($name,$age);
$this->score=$score;
}
public function Introduce()
{
parent::SayHello();
echo(".In this exam,I got ".$this->score);
}
function __destruct()
{
echo("我要被卸载了");
}
}

6. 多态

由于默认参数的存在,以及PHP的弱类型,使得编译时多态(也就是由于参数个数以及类型不同而造成的多态)无法实现,但是运行时多态在上文中已有提及。不再赘述。
相关文章 大家在看