Blogger Information
Blog 36
fans 1
comment 0
visits 26093
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示类的扩展、抽象和接口 命名空间概念与作用
早晨
Original
551 people have browsed it

类的扩展、抽象和接口实例代码

  1. <?php
  2. namespace _0815;
  3. // 父级类
  4. class Ab
  5. {
  6. protected $name;
  7. public function __construct($name)
  8. {
  9. $this->name = $name;
  10. }
  11. protected function getInfo(): string
  12. {
  13. return $this->name;
  14. }
  15. }
  16. // 继承父级类
  17. class Cd extends Ab
  18. {
  19. private $sex;
  20. private $nation;
  21. public function __construct($name, $sex, $nation)
  22. {
  23. parent::__construct($name);
  24. $this->sex = $sex;
  25. $this->nation = $nation;
  26. }
  27. public function getInfo(): string
  28. {
  29. return '姓名:' . parent::getInfo() . '---->性别:' . $this->sex . '---->国籍: ' . $this->nation . '<br>';
  30. }
  31. }
  32. $cd = new Cd('早晨', '男', '中国');
  33. echo '-----------------继承类-----------------------------------<br>';
  34. echo $cd->getInfo() . '<br>';
  35. // 抽象类
  36. abstract class User1
  37. {
  38. public $name = '小明同学';
  39. abstract public static function getInfo($name);
  40. }
  41. class User2 extends User1
  42. {
  43. public static function getInfo($name)
  44. {
  45. return 'Hello, ' . $name;
  46. }
  47. }
  48. echo '-----------------抽象类-----------------------------------<br>';
  49. echo User2::getInfo('早上好') . '<br>';
  50. // 接口类
  51. interface iWeixin
  52. {
  53. public function WeixinName();
  54. }
  55. // 继承接口
  56. class Weixin implements iWeixin
  57. {
  58. public $wx_name = '微信API';
  59. public function WeixinName()
  60. {
  61. return '接口名称:' . $this->wx_name;
  62. }
  63. }
  64. echo '-----------------接口类-----------------------------------<br>';
  65. $api_weixin = new Weixin();
  66. echo $api_weixin->WeixinName();

运行效果

全局成员有三种:类、函数、常量,特点是不能重复声明。

namespace即“命名空间”,也称“名称空间” 、”名字空间”。如果两个人写的库文件中出现同名的变量或函数(不可避免),使用起来就有问题了。为了解决这个问题,引入了名字空间这个概念。
命名空间:实际上就是一个由程序设计者命名的内存区域,程序设计者可以根据需要指定一些有名字的空间域,把一些全局实体分别放在各个命名空间中,从而与其他全局实体分隔开来。
命名空间的作用:是建立一些互相分隔的作用域,把一些全局实体分隔开来。命名空间的作用类似于操作系统中的目录和文件的关系,由于文件很多,不便管理,而且容易重名,于是人们设立若干子目录,把文件分别放到不同的子目录中,不同子目录中的文件可以同名。调用文件时应指出文件路径。

跨空间成员访问方式实例代码

  1. <?php
  2. // 跨空间成员访问方式实例
  3. namespace one;
  4. class Username1
  5. {
  6. public static $name = '早晨';
  7. }
  8. echo Username1::class . '<br>';
  9. echo \two\Username1::$name . '<hr>';
  10. namespace two;
  11. class Username1
  12. {
  13. public static string $name = '我叫早晨';
  14. }
  15. echo Username1::class . '<br>';
  16. echo \one\Username1::$name . '<hr>';

运行效果

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!