Blogger Information
Blog 34
fans 2
comment 0
visits 23168
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月30号作业 抽象类与接口
遗忘了寂寞
Original
581 people have browsed it

抽象类与接口

  1. <?php
  2. //抽象类
  3. abstract class a{
  4. public $name;
  5. public function __construct($name){
  6. $this->name=$name;
  7. }
  8. public function af(){
  9. echo $this->name;
  10. }
  11. //抽象方法不能有内容
  12. abstract function aff();
  13. }
  14. class b extends a{
  15. //b继承a后,必须把a里面的抽象方法实现
  16. public function aff(){
  17. echo $this->name;
  18. }
  19. }
  20. $a =new b('张三');
  21. $a->af();

  1. <?php
  2. //接口
  3. interface ivehicle{
  4. const CUNTRY='中国';
  5. public function setFuel($fuel);
  6. public function setPurpose($purpose);
  7. }
  8. //接口不可以有普通方法,不可以有成员变量
  9. class car implements ivehicle{
  10. public $fuel;
  11. public $purpose;
  12. public function __construct($fuel='汽油',$purpose='家用'){
  13. $this->fuel=$fuel;
  14. $this->purpose=$purpose;
  15. }
  16. public function setFuel($fuel){
  17. $this->fuel=$fuel;
  18. }
  19. public function setPurpose($purpose){
  20. $this->purpose=$purpose;
  21. }
  22. public function getInfo(){
  23. return $this->fuel . $this->purpose . '车<br>';
  24. }
  25. }
  26. $car=new Car();
  27. echo $car->getInfo();
  28. $car->setFuel('新能源');
  29. $car->setPurpose('公交');
  30. echo $car->getInfo();



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