博客列表 >举例实现trait的应用场景和简单演示双色球的实战(0723)

举例实现trait的应用场景和简单演示双色球的实战(0723)

丶久而旧之丶
丶久而旧之丶原创
2020年08月19日 13:54:08503浏览

trait

通过引入trait实现代码复用

  1. <?php
  2. trait tDemo
  3. {
  4. public function show()
  5. {
  6. // 打印类中所有属性
  7. return printf("<pre>%s</pre>", print_r(get_class_vars(__CLASS__), true));
  8. }
  9. }
  10. class User
  11. {
  12. use tDemo;
  13. protected $name = '小明';
  14. protected $sex = '男';
  15. }
  16. class User1
  17. {
  18. use tDemo;
  19. protected $name = '小熊';
  20. protected $sex = '男';
  21. }
  22. (new User)->show();
  23. echo '<hr>';
  24. (new User1)->show();

trait的继承应用

  1. <?php
  2. // trait的继承应用
  3. trait tDemo
  4. {
  5. public static function show()
  6. {
  7. // 打印当前类中的方法
  8. return "trait中的方法" . __METHOD__;
  9. }
  10. }
  11. // 父类
  12. abstract class Dad
  13. {
  14. public static function show()
  15. {
  16. return "父类中的方法:" . __METHOD__;
  17. }
  18. }
  19. // 子类(继承父类和引入trait)
  20. class Son extends Dad
  21. {
  22. use tDemo;
  23. public static function show()
  24. {
  25. return "子类中的方法:" . __METHOD__;
  26. }
  27. }
  28. echo Son::show();

如果子类,父类和trait中有同名方法那么调用时优先级子类>trait>父类

trait的扩展

  1. <?php
  2. trait tDemo
  3. {
  4. public static function show()
  5. {
  6. // 打印类中的全部属性
  7. printf("<pre>%s</pre>", print_r(get_class_vars(__CLASS__), true));
  8. }
  9. }
  10. trait tDemo1
  11. {
  12. public static function show1()
  13. {
  14. // 打印类中的全部方法
  15. printf("<pre>%s</pre>", print_r(get_class_methods(__CLASS__), true));
  16. }
  17. }
  18. // 通过引入多个trait实现类的扩展功能
  19. // 也可以通过一个trait整合其他trait,然后通过引入这个trait实现引入多个stait
  20. trait tDemo2
  21. {
  22. use tDemo, tDemo1;
  23. }
  24. class Work
  25. {
  26. // 引入多个trait
  27. use tDemo,tDemo1;
  28. // 引入一个整合的trait
  29. use tDemo2;
  30. public $name = '麻辣小龙虾虾虾';
  31. public $money = '38元/斤';
  32. public function get()
  33. {
  34. return $this->name . ':' . $this->money;
  35. }
  36. }
  37. echo (new Work)->get(), '<hr>';
  38. echo (new Work)->show(), '<hr>';
  39. echo (new Work)->show1(), '<hr>';

trait命名冲突解决方案

  1. <?php
  2. trait tDemo
  3. {
  4. public function get()
  5. {
  6. return __METHOD__;
  7. }
  8. }
  9. trait tDemo2
  10. {
  11. public function get()
  12. {
  13. return __METHOD__;
  14. }
  15. }
  16. // 也可以把所有trait整合
  17. trait tDemo3
  18. {
  19. use tDemo, tDemo2 {
  20. // 给tDemo2中的同名方法取一个别名
  21. tDemo2::get as tg2;
  22. // 把tDemo中的get方法替代掉tDemo中的get方法
  23. tDemo::get insteadof tDemo2;
  24. }
  25. }
  26. // 工作类越简单越好
  27. class Work
  28. {
  29. use tDemo3;
  30. }
  31. echo (new Work)->get(), '<hr>';
  32. // 别名调用方法
  33. echo (new Work)->tg2();

接口和trait的组合应用

  1. <?php
  2. // 可以把接口中的抽象方法在trait中实现然后在工作类中引入trait
  3. // 接口
  4. interface iDemo
  5. {
  6. public static function get();
  7. }
  8. // trait中实现接口中的抽象方法
  9. trait tDemo
  10. {
  11. public static function get()
  12. {
  13. return __METHOD__;
  14. }
  15. }
  16. // 工作类(实现接口并引入trait)
  17. class Demo implements iDemo
  18. {
  19. use tDemo;
  20. }
  21. echo Demo::get();

双色球演示

  1. <?php
  2. // 接口
  3. interface intId
  4. {
  5. public static function getId($min, $max);
  6. }
  7. // 实现接口方法
  8. trait trId
  9. {
  10. public static function getId($min, $max)
  11. {
  12. return mt_rand($min, $max);
  13. }
  14. }
  15. // 创建抽象类引入接口和接口的实现方法
  16. abstract class Lottry implements intId
  17. {
  18. use trID;
  19. // 生成中奖所需号码
  20. protected static function createBalls($min, $max, $num)
  21. {
  22. // 生成中奖号码范围(数组)
  23. $allballs = range($min, $max, 1);
  24. // 根据数量决定是红球还是蓝球
  25. if ($num == 1) return $allballs[array_rand($allballs)];
  26. $redballs = [];
  27. // 获取键名并遍历获取值
  28. foreach (array_rand($allballs, $num) as $key) {
  29. $redballs[] = $allballs[$key];
  30. }
  31. return $redballs;
  32. }
  33. // 中奖方法
  34. abstract protected static function doubleBall($redballs, $blueball);
  35. // 试机号
  36. abstract protected static function shiji($redballs, $buleball, $range);
  37. }
  38. // 子类(工作类)
  39. class Work extends Lottry
  40. {
  41. // 实现抽象类的中奖方法
  42. public static function doubleBall($redballs, $blueball)
  43. {
  44. // 调用抽象类中的方法获取红蓝球的值
  45. $red = self::createBalls(...$redballs);
  46. $blue = self::createBalls(...$blueball);
  47. // 合并
  48. array_push($red, $blue);
  49. return $red;
  50. }
  51. // 实现抽象类中的试机方法
  52. public static function shiji($redballs, $buleball, $range)
  53. {
  54. // 随机生成试机的数量
  55. $count = self::getId(...$range);
  56. // 调用中奖方法
  57. for ($i = 0; $i < $count; $i++) {
  58. $red[] = self::doubleball($redballs, $buleball);
  59. }
  60. return $red;
  61. }
  62. }
  63. $a = Work::doubleBall([1, 33, 6], [1, 16, 1]);
  64. $b = Work::shiji([1, 33, 6], [1, 16, 1], [1, 5]);
  65. ?>
  66. <!DOCTYPE html>
  67. <html lang="en">
  68. <head>
  69. <meta charset="UTF-8">
  70. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  71. <title>Document</title>
  72. <style>
  73. .container {
  74. display: flex;
  75. }
  76. .container>.ball {
  77. display: flex;
  78. width: 30px;
  79. height: 30px;
  80. justify-content: center;
  81. align-items: center;
  82. border-radius: 50%;
  83. margin: 5px;
  84. color: white;
  85. box-shadow: 8px 8px 16px lightgray;
  86. }
  87. .container>.ball {
  88. background-color: red;
  89. }
  90. .container>.ball:last-of-type {
  91. background-color: blue;
  92. }
  93. </style>
  94. </head>
  95. <body>
  96. <h2>今日开奖号码</h2>
  97. // 遍历结果集
  98. <div class='container'>
  99. <?php foreach ($a as $key) : ?>
  100. <span class='ball'><?= $key ?></span>
  101. <?php endforeach; ?>
  102. </div>
  103. <h2>今日试机号码</h2>
  104. <?php foreach ($b as $key) : ?>
  105. <div class='container'>
  106. <?php foreach ($key as $k) : ?>
  107. <sapn class='ball'><?= $k ?></sapn>
  108. <?php endforeach ?>
  109. </div>
  110. <?php endforeach ?>
  111. </body>
  112. </html>

总结

1.了解了trait中的各种应用场景
2.了解了trait和接口,抽象类之间的区别

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议