Blogger Information
Blog 36
fans 1
comment 0
visits 28831
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月29日练习抽象类和接口-九期线上班
WJF
Original
437 people have browsed it

抽象类


  1. <?php
  2. //抽象类
  3. //类中只要有抽象方法 就声明为抽象类
  4. //抽象类只能被继承 不可以被实例化
  5. abstract class P{
  6. // protected 受保护的 只能本类和子类 父类中访问
  7. protected $name;
  8. public function __construct($name)
  9. {
  10. $this->name = $name;
  11. }
  12. public function getname(){
  13. return $this->name;
  14. }
  15. abstract public function setname($value);
  16. }
  17. class S extends P{
  18. public function __construct($name){
  19. parent::__construct($name);
  20. }
  21. public function setname($value)
  22. {
  23. $this->name = $value;
  24. }
  25. }
  26. $s = new S('33703259');
  27. echo $s->getname();
  28. echo '<hr>';
  29. $s->setname('WJF');
  30. echo $s->getname();



接口


  1. <?php
  2. //接口
  3. //指定某个类重必须实现的方法 接口中不需要定义方法的具体实现过程
  4. //接口只允许出现 方法 类变量
  5. //可见性必须为 public 方法体必须为空
  6. interface V{
  7. public function A($A);
  8. public function B($B);
  9. }
  10. class C implements V{
  11. public $A;
  12. public $B;
  13. public function A($A){
  14. $this->A = $A;
  15. }
  16. public function B($B)
  17. {
  18. $this->B = $B;
  19. }
  20. public function DIY(){
  21. return 'A值: ' . $this->A . 'B值: ' . $this->B . 'PHP中文网';
  22. }
  23. }
  24. $c = new C();
  25. $c->A('aaaaa');
  26. $c->B('BBBBB');
  27. echo $c->DIY();



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