Discuz教程网

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

[复制链接]
authicon dly 发表于 2011-9-6 11:11:31 | 显示全部楼层 |阅读模式
但是我们知道,面向对象有三大特征:继承,多态和封装。

1. 继承

我们继续上一节中的例子,在PHP中,继承和Java是一样的,都使用extends关键字。
代码如下:

  1. class People
  2. {
  3. private $name;
  4. public function GetName()
  5. {
  6. return $this->name;
  7. }
  8. public function SetName($name)
  9. {
  10. $this->name=$name;
  11. }
  12. }
  13. class Student extends People
  14. {
  15. private $grade;
  16. public function SayHello()
  17. {
  18. echo("Good Morning,".parent::GetName());
  19. }
  20. }
复制代码


在这里,我们需要主要的还有我们访问父类在C# 中用base,在Java中用super,但是在PHP中,我们用parent关键字。

如果我们要访问自身的方法,那么可以用this,也可以用self。
代码如下:

  1. class Student extends People
  2. {
  3. public function GetName()
  4. {
  5. return "kym";
  6. }
  7. private $grade;
  8. public function SayHello()
  9. {
  10. echo("Good Morning,".self::GetName());
  11. //echo("Good Morning,".$this->GetName());
  12. }
  13. }
复制代码


2. 抽象类

提到继承,就不得不说抽象类。
代码如下:

  1. <?php
  2. abstract class People
  3. {
  4. private $name;
  5. public function GetName()
  6. {
  7. return $this->name;
  8. }
  9. public function SetName($name)
  10. {
  11. $this->name=$name;
  12. }
  13. abstract function SayHello();
  14. }
  15. class Student extends People
  16. {
  17. public function SayHello()
  18. {
  19. echo("Good Morning,".parent::GetName());
  20. }
  21. }
  22. $s=new Student();
  23. $s->SetName("kym");
  24. $s->SayHello();
  25. ?>
复制代码



3. 接口

接下来就是接口:
代码如下:

  1. <?php
  2. abstract class People
  3. {
  4. private $name;
  5. public function GetName()
  6. {
  7. return $this->name;
  8. }
  9. public function SetName($name)
  10. {
  11. $this->name=$name;
  12. }
  13. abstract function SayHello();
  14. }
  15. interface IRun
  16. {
  17. function Run();
  18. }
  19. class Student extends People implements IRun
  20. {
  21. public function SayHello()
  22. {
  23. echo("Good Morning,".parent::GetName());
  24. }
  25. public function Run()
  26. {
  27. echo("两条腿跑");
  28. }
  29. }
  30. $s=new Student();
  31. $s->SetName("kym");
  32. $s->SayHello();
  33. $s->Run();
  34. ?>
复制代码


都没什么好说的,跟Java一模一样。

4. 构造方法

一直忘了说构造方法,其实也就是一段同样的代码:
代码如下:

  1. <?php
  2. class Person
  3. {
  4. private $name;
  5. private $age;
  6. public function Person($name,$age)
  7. {
  8. $this->name=$name;
  9. $this->age=$age;
  10. }
  11. public function SayHello()
  12. {
  13. echo("Hello,My name is ".$this->name.".I'm ".$this->age);
  14. }
  15. }
  16. $p=new Person("kym",22);
  17. $p->SayHello();
  18. ?>
复制代码



我们在面试中也许经常会遇到一种变态的题型,就是若干个类之间的关系,然后构造函数呀什么的调来调去。但是,在PHP中就不会遇到这样的情况了,因为在PHP中并不支持构造函数链,也就是说,在你初始化子类的时候,他不会自动去调用父类的构造方法。
代码如下:

  1. <?php
  2. class Person
  3. {
  4. private $name;
  5. private $age;
  6. public function Person($name,$age)
  7. {
  8. $this->name=$name;
  9. $this->age=$age;
  10. }
  11. public function SayHello()
  12. {
  13. echo("Hello,My name is ".$this->name.".I'm ".$this->age);
  14. }
  15. }
  16. class Student extends Person
  17. {
  18. private $score;
  19. public function Student($name,$age,$score)
  20. {
  21. $this->Person($name,$age);
  22. $this->score=$score;
  23. }
  24. public function Introduce()
  25. {
  26. parent::SayHello();
  27. echo(".In this exam,I got ".$this->score);
  28. }
  29. }

  30. $s=new Student("kym",22,120);
  31. $s->Introduce();
  32. ?>
复制代码




5. 析构函数

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

  1. class Student extends Person
  2. {
  3. private $score;
  4. public function Student($name,$age,$score)
  5. {
  6. $this->Person($name,$age);
  7. $this->score=$score;
  8. }
  9. public function Introduce()
  10. {
  11. parent::SayHello();
  12. echo(".In this exam,I got ".$this->score);
  13. }
  14. function __destruct()
  15. {
  16. echo("我要被卸载了");
  17. }
  18. }
复制代码




6. 多态

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




上一篇:Discuz X2 头像无法显示
下一篇:PHP一步一步学习(5) 类和对象
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 01:30

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表