Blogger Information
Blog 62
fans 3
comment 1
visits 29684
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php类与命名空间
kiraseo_wwwkiraercom
Original
297 people have browsed it

实例演示类的扩展,抽象,接口的语法

案例代码

  1. <?php
  2. namespace _0815;
  3. class Zuoye {
  4. public function __construct($name)
  5. {
  6. $this->name = $name;
  7. }
  8. }
  9. class Name extends Zuoye
  10. {
  11. public function __construct($name,$age)
  12. {
  13. parent::__construct($name);
  14. $this->age = $age;
  15. }
  16. public function getOut(){
  17. return $this->name .'同学,年龄'. $this->age.'岁';
  18. }
  19. }
  20. echo "1. 实例演示类的扩展,抽象,接口的语法<br/>";
  21. echo "类的扩展<br/>";
  22. $res = new Name( '小明',18);
  23. echo $res->getOut();
  24. echo "<br/>";
  25. abstract class Myname
  26. {
  27. public string $name = 'kiraer';
  28. abstract public function getName($name);
  29. }
  30. class Mz extends Myname
  31. {
  32. public function getName($name)
  33. {
  34. return $name.'同学你好';
  35. }
  36. }
  37. echo "类的抽象<br/>";
  38. $my = new Mz();
  39. echo $my->getName($my->name);
  40. echo "<br/>";
  41. interface iNickname
  42. {
  43. public function getMyname($name);
  44. public function getMYage($age);
  45. }
  46. class Web implements iNickname
  47. {
  48. public function getMyname($name)
  49. {
  50. return '你好'.$name.'同学';
  51. }
  52. public function getMYage($age)
  53. {
  54. return '今年'.$age.'岁';
  55. }
  56. }
  57. echo "接口<br/>";
  58. $name = new Web();
  59. echo $name->getMyname('小王').','. $name->getMYage('15');
  60. echo "<br/>";

效果图

全局成员有哪些,他们有哪些特点?为什么要用命名空间, 描述命名空间的作用,以及声明方 式, 跨空间成员的访问方式. 全局成员有哪些,他们有哪些特点?为什么要用命名空间, 描述 命名空间的作用,以及声明方式, 跨空间成员的访问方式

案例代码

  1. <?php
  2. namespace _0815_1;
  3. echo "2. 全局成员有哪些,他们有哪些特点?为什么要用命名空间, 描述命名空间的作用,以及声明方式, 跨空间成员的访问方式";
  4. echo "<br/>";
  5. echo "全局成员有函数,常量,类/接口,全局都可以用,但是不能重复";
  6. echo "<br/>";
  7. echo "描述命名空间的作用,可以解决全局成员重复的问题";
  8. echo "<br/>";
  9. class One
  10. {
  11. public function show(){
  12. return __METHOD__;
  13. }
  14. }
  15. $res = new One();
  16. echo $res->show();
  17. echo "<br/>";
  18. namespace _0815_2;
  19. class One
  20. {
  21. public function show(){
  22. return __METHOD__;
  23. }
  24. }
  25. $res = new One();
  26. echo $res->show();
  27. echo "<br/>";
  28. namespace _0815_3;
  29. class One
  30. {
  31. public function show(){
  32. return __METHOD__;
  33. }
  34. }
  35. namespace _0815_4;
  36. class One
  37. {
  38. public function show(){
  39. return __METHOD__;
  40. }
  41. }
  42. echo "跨空间成员的访问方式";
  43. echo "<br/>";
  44. use _0815_3\One as one_index;
  45. $res_one =new one_index();
  46. echo $res_one->show();

效果图

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