Blogger Information
Blog 18
fans 0
comment 0
visits 13237
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP基础知识接口与trait的基本功能学习小结
马晓宁
Original
711 people have browsed it

1.接口继承

  1. <?php
  2. interface testA{
  3. function echostr();
  4. }
  5. interface testB{
  6. function dancing($name);
  7. }
  8. interface testC extends testA,testB{
  9. function singing($nickname);
  10. }
  11. class testD implements testC{
  12. function echostr(){
  13. echo "接口继承,要实现父接口所有相关方法!";
  14. echo "<br />";
  15. }
  16. function dancing($name){
  17. echo $name."正在读书!";
  18. echo "<br />";
  19. }
  20. function singing($nickname){
  21. echo $nickname."正在运动!";
  22. }
  23. }
  24. $demo=new testD();
  25. $demo->echostr();
  26. $demo->dancing("小红");
  27. $demo->singing("小张");
  28. ?>

结果:
接口继承,要实现父接口所有相关方法!
小红正在读书!
小张正在运动!


2.trait的简单用法

  1. <?php
  2. trait dog{
  3. public $name = 'dog';
  4. public function run(){
  5. echo "dog run";
  6. }
  7. }
  8. class Animal{
  9. public function eat(){
  10. echo "animal eat";
  11. }
  12. }
  13. class cat extends Animal{
  14. use dog;
  15. public function play(){
  16. echo "cat play";
  17. }
  18. }
  19. $cat = new cat();
  20. $cat->play();
  21. echo "<br/>";
  22. $cat->eat();
  23. echo "<br/>";
  24. $cat->run();
  25. ?>

结果:
cat play
animal eat
dog run


总结:还是觉得有点难了,有的地方还是不十分明白。只是做了简单示例。还需要多多巩固练习。

Correcting teacher:天蓬老师天蓬老师

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