Blogger Information
Blog 36
fans 1
comment 0
visits 28785
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月03日类与对象复习-九期线上班
WJF
Original
645 people have browsed it

手抄笔记






练习案例


demo1:
  1. <?php
  2. //创建类
  3. class Demo1{
  4. //添加类成员
  5. //属性
  6. public $name = '33703259';
  7. //方法
  8. public function GET(){
  9. $obj = new Demo1();
  10. return '名称:' . $obj->name . '<hr>';
  11. }
  12. }
  13. //访问类成员
  14. $obj =new Demo1();
  15. echo $obj->GET();
  16. echo $obj->name;

demo2:
  1. <?php
  2. //创建类
  3. class Demo2{
  4. //添加类成员
  5. public $name = 'WJF';
  6. public $qq = '33703259';
  7. public function getname(){
  8. // self当前类名
  9. $obj = new self();
  10. return '用户名:' . $obj->name . '<hr>';
  11. }
  12. public function getqq(){
  13. // $this 当前类的实例引用
  14. return 'QQ:' . $this->qq;
  15. }
  16. }
  17. //访问类成员
  18. $obj = new Demo2();
  19. echo $obj->getname();
  20. echo $obj->getqq();

demo3:
  1. <?php
  2. class Demo3{
  3. public $name;
  4. public $qq;
  5. public function get(){
  6. return '用户名:' . $this->name . 'QQ:' . $this->qq;
  7. }
  8. public function __construct($name,$qq)
  9. {
  10. $this->name = $name;
  11. $this->qq = $qq;
  12. echo $this->get();
  13. }
  14. }
  15. new Demo3('用户名','QQ');

demo4:
  1. <?php
  2. //创建类
  3. class d4{
  4. //添加类成员
  5. public $name;
  6. protected $qq;
  7. public function get(){
  8. return '用户名:' . $this->name .'QQ:' . $this->qq;
  9. }
  10. // 构造方法
  11. public function __construct($name,$qq)
  12. {
  13. $this->name = $name;
  14. $this->qq = $qq;
  15. }
  16. //魔术方法 __get($name),属性重载
  17. public function __get($name)
  18. {
  19. $id = $_GET['id'] ?? '';
  20. if (isset($id) and $id==='1'){
  21. return isset($this->qq) ? $this->qq : '当前暂无该用户QQ';
  22. }else{
  23. return '您不符合当前操作权限要求';
  24. }
  25. }
  26. }
  27. //访问类成员
  28. $obj = new d4('wjf','33703259');
  29. echo $obj->qq;

demo5:
  1. <?php
  2. //类的继承
  3. class d5{
  4. public $name;
  5. public $qq;
  6. public function get(){
  7. return '用户名:' . $this->name .'QQ:' . $this->qq;
  8. }
  9. // 构造方法
  10. public function __construct($name,$qq)
  11. {
  12. $this->name = $name;
  13. $this->qq = $qq;
  14. }
  15. }
  16. class d5_1 extends d5{
  17. private $zt;
  18. public function __construct($name, $qq,$zt)
  19. {
  20. parent::__construct($name, $qq);
  21. $this->zt = $zt;
  22. }
  23. public function get()
  24. {
  25. return parent::get() . '状态:' . $this->zt;
  26. }
  27. }
  28. $obj = new d5_1('WJF','33703259','在线');
  29. echo $obj->get();

demo6:
  1. <?php
  2. //trait:代码复用
  3. trait T{
  4. public function get(){
  5. return '用户名:' . $this->name .'QQ:' . $this->qq;
  6. }
  7. }
  8. class d6{
  9. use T;
  10. public $name;
  11. public $qq;
  12. public function __construct($name,$qq)
  13. {
  14. $this->name = $name;
  15. $this->qq = $qq;
  16. }
  17. }
  18. class d6_1 extends d6{
  19. private $zt;
  20. public function __construct($name, $qq,$zt)
  21. {
  22. parent::__construct($name, $qq);
  23. $this->zt = $zt;
  24. }
  25. public function get()
  26. {
  27. return parent::get() . '状态:' . $this->zt;
  28. }
  29. }
  30. $obj = new d6_1('WJF','33703259','离线');
  31. echo $obj->get();

demo7:
  1. <?php
  2. //接口
  3. interface idemo{
  4. public function get();
  5. }
  6. class d7 implements idemo{
  7. public $name;
  8. public $qq;
  9. public function get(){
  10. return '用户名:' . $this->name .'QQ:' . $this->qq;
  11. }
  12. public function __construct($name,$qq)
  13. {
  14. $this->name = $name;
  15. $this->qq = $qq;
  16. }
  17. }
  18. $obj = new d7('WJF','33703259');
  19. echo $obj->get();

demo8:
  1. <?php
  2. //抽象类
  3. abstract class cx{
  4. //抽象方法
  5. abstract public function get();
  6. //已实现的方法
  7. public function h(){
  8. return '已实现方法';
  9. }
  10. }
  11. class d8 extends cx {
  12. public $name;
  13. public $qq;
  14. public function get(){
  15. return '用户名:' . $this->name .'QQ:' . $this->qq;
  16. }
  17. public function __construct($name,$qq)
  18. {
  19. $this->name = $name;
  20. $this->qq = $qq;
  21. }
  22. }
  23. $obj = new d8('WJF','33703259');
  24. echo $obj->get();
  25. echo $obj->h();
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