但是我们知道,面向对象有三大特征:继承,多态和封装。
1. 继承
我们继续上一节中的例子,在PHP中,继承和Java是一样的,都使用extends关键字。
复制代码 代码如下:
class People
{
private $name;
public function GetName()
{
return $this->name;
}
public function SetName($name)
{
$this->name=$name;
}
}
class Student extends People
{
private $grade;
public function SayHello()
{
echo("Good Morning,".parent::GetName());
}
}
在这里,我们需要主要的还有我们访问父类在C# 中用base,在Java中用super,但是在PHP中,我们用parent关键字。
如果我们要访问自身的方法,那么可以用this,也可以用self。
复制代码 代码如下:
class Student extends People
{
public function GetName()
{
return "kym";
}
private $grade;
public function SayHello()
{
echo("Good Morning,".self::GetName());
//echo("Good Morning,".$this->GetName());
}
}
2. 抽象类
提到继承,就不得不说抽象类。
复制代码 代码如下:
<?php
abstract class People
{
private $name;
public function GetName()
{
return $this->name;
}
public function SetName($name)
{
$this->name=$name;
}
abstract function SayHello();
}
class Student extends People
{
public function SayHello()
{
echo("Good Morning,".parent::GetName());
}
}
$s=new Student();
$s->SetName("kym");
$s->SayHello();
?>
3. 接口
接下来就是接口:
复制代码 代码如下:
<?php
abstract class People
{
private $name;
public function GetName()
{
return $this->name;
}
public function SetName($name)
{
$this->name=$name;
}
abstract function SayHello();
}
interface IRun
{
function Run();
}
class Student extends People implements IRun
{
public function SayHello()
{
echo("Good Morning,".parent::GetName());
}
public function Run()
{
echo("两条腿跑");
}
}
$s=new Student();
$s->SetName("kym");
$s->SayHello();
$s->Run();
?>
都没什么好说的,跟Java一模一样。
4. 构造方法
一直忘了说构造方法,其实也就是一段同样的代码:
复制代码 代码如下:
<?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);
}
}
$p=new Person("kym",22);
$p->SayHello();
?>
我们在面试中也许经常会遇到一种变态的题型,就是若干个类之间的关系,然后构造函数呀什么的调来调去。但是,在PHP中就不会遇到这样的情况了,因为在PHP中并不支持构造函数链,也就是说,在你初始化子类的时候,他不会自动去调用父类的构造方法。
复制代码 代码如下:
<?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的弱类型,使得编译时多态(也就是由于参数个数以及类型不同而造成的多态)无法实现,但是运行时多态在上文中已有提及。不再赘述。
1. 继承
我们继续上一节中的例子,在PHP中,继承和Java是一样的,都使用extends关键字。
复制代码 代码如下:
class People
{
private $name;
public function GetName()
{
return $this->name;
}
public function SetName($name)
{
$this->name=$name;
}
}
class Student extends People
{
private $grade;
public function SayHello()
{
echo("Good Morning,".parent::GetName());
}
}
在这里,我们需要主要的还有我们访问父类在C# 中用base,在Java中用super,但是在PHP中,我们用parent关键字。
如果我们要访问自身的方法,那么可以用this,也可以用self。
复制代码 代码如下:
class Student extends People
{
public function GetName()
{
return "kym";
}
private $grade;
public function SayHello()
{
echo("Good Morning,".self::GetName());
//echo("Good Morning,".$this->GetName());
}
}
2. 抽象类
提到继承,就不得不说抽象类。
复制代码 代码如下:
<?php
abstract class People
{
private $name;
public function GetName()
{
return $this->name;
}
public function SetName($name)
{
$this->name=$name;
}
abstract function SayHello();
}
class Student extends People
{
public function SayHello()
{
echo("Good Morning,".parent::GetName());
}
}
$s=new Student();
$s->SetName("kym");
$s->SayHello();
?>
3. 接口
接下来就是接口:
复制代码 代码如下:
<?php
abstract class People
{
private $name;
public function GetName()
{
return $this->name;
}
public function SetName($name)
{
$this->name=$name;
}
abstract function SayHello();
}
interface IRun
{
function Run();
}
class Student extends People implements IRun
{
public function SayHello()
{
echo("Good Morning,".parent::GetName());
}
public function Run()
{
echo("两条腿跑");
}
}
$s=new Student();
$s->SetName("kym");
$s->SayHello();
$s->Run();
?>
都没什么好说的,跟Java一模一样。
4. 构造方法
一直忘了说构造方法,其实也就是一段同样的代码:
复制代码 代码如下:
<?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);
}
}
$p=new Person("kym",22);
$p->SayHello();
?>
我们在面试中也许经常会遇到一种变态的题型,就是若干个类之间的关系,然后构造函数呀什么的调来调去。但是,在PHP中就不会遇到这样的情况了,因为在PHP中并不支持构造函数链,也就是说,在你初始化子类的时候,他不会自动去调用父类的构造方法。
复制代码 代码如下:
<?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的弱类型,使得编译时多态(也就是由于参数个数以及类型不同而造成的多态)无法实现,但是运行时多态在上文中已有提及。不再赘述。
标签:
PHP,面向对象
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金钱帮资源网 Copyright www.kbjia.com
暂无“一步一步学习PHP(6) 面向对象”评论...
更新日志
2024年10月14日
2024年10月14日
- 群星《歌手2024 第4期》[FLAC/分轨][563.76MB]
- RiffKitten-ChaosParade(2024)[24-44,1]FLAC
- RuneKlakegg-Nattevandrer(2024)[24-96]\FLAC
- 赖冰霞.2006-金嗓典藏辑2IN1(南方金点系列)2CD【南方】【WAV+CUE】
- 群星.1988-国语金曲尽精英VOL.2【瑞成】【WAV+CUE】
- 张宇.1999-雨一直下【EMI百代】【WAV+CUE】
- 袁娅维.2021-月亮失眠了(DELUXE)【华纳】【FLAC分轨】
- AlanBroadbent-JazzFunk(2024)[24-44,1]
- 梅艳芳《绝代芳华》开盘母带[低速原抓WAV+CUE]
- ABC唱片-《外国精逊母带直刻神奇黑胶[APE+CUE].
- 群星《天赐的声音第五季 第6期》[320K/MP3][101.43MB]
- 群星《天赐的声音第五季 第6期》[FLAC/分轨][526.16MB]
- 群星《说唱梦工厂 第2期》[320K/MP3][96.37MB]
- 杨宗宪.1994-留你留袂着【有容唱片】【WAV+CUE】
- 李翊君.2005-勇敢的爱【创意唱片】【WAV+CUE】