Blogger Information
Blog 42
fans 5
comment 0
visits 38589
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php复习第二天,类与对象
张浩刚
Original
785 people have browsed it

基础部分

  1. <?php
  2. class demo{
  3. public $name;
  4. protected $age;
  5. private $country = '中国';
  6. public function get(){
  7. return $this->name . $this->age . $this->country;
  8. }
  9. public function __construct($a,$b)
  10. {
  11. $this->name = $a;
  12. $this->age = $b;
  13. }
  14. }
  15. $obj = new demo('张明轩',3);
  16. echo $obj->name;
  17. echo $obj->get();

trait公共区,子类继承

  1. <?php
  2. // trait 公共区
  3. trait text{
  4. public function get(){
  5. return $this->name . $this->age . $this->country;
  6. }
  7. }
  8. class demo{
  9. public $name;
  10. protected $age;
  11. private $country = '中国';
  12. // use 调用公共区
  13. use text;
  14. public function __construct($a,$b)
  15. {
  16. $this->name = $a;
  17. $this->age = $b;
  18. }
  19. }
  20. class sun extends demo{
  21. public function __construct($a,$b,$c)
  22. {
  23. //子类 继承父类
  24. parent::__construct($a,$b);
  25. $this->c = $c;
  26. }
  27. public function get()
  28. {
  29. return parent::get() . $this->c . ' 这是子类调用';
  30. }
  31. }
  32. $str = new sun('张明轩',3,'love');
  33. echo $str->get();

interface接口

  1. <?php
  2. // 接口是一种约定, 定义了实现它的类中必须实现的方法
  3. // 接口中没有方法的具体实现, 所以不能实例化
  4. interface iDemo{
  5. public function get();
  6. public function hello();
  7. }
  8. class demo implements iDemo{
  9. public $name;
  10. protected $age;
  11. private $country = '中国';
  12. public function get(){
  13. return $this->name . $this->age . $this->country;
  14. }
  15. public function hello(){
  16. echo '接口文件,在类中必须一一呈现';
  17. }
  18. public function __construct($a,$b)
  19. {
  20. $this->name = $a;
  21. $this->age = $b;
  22. }
  23. }
  24. class sun extends demo{
  25. public function __construct($a,$b,$c)
  26. {
  27. //子类 继承父类
  28. parent::__construct($a,$b);
  29. $this->c = $c;
  30. }
  31. //这里info()可以和父类get(),名字一致
  32. public function info()
  33. {
  34. return parent::get() . $this->c . ' 这是子类调用';
  35. }
  36. }
  37. $cc = new demo('张明轩',3,);
  38. $cc->hello();
  39. $str = new sun('张明轩',3,'love');
  40. echo $str->info();

abstract抽象

  1. <?php
  2. // 抽象类:有抽象方法, 也是已实现的方法
  3. // 接口全是抽象方法
  4. // 共同之处: 统统不能实例化, 原因就是内部有抽象方法
  5. abstract class iDemo{
  6. public function hello(){
  7. echo '抽象类:已经实现的方法';
  8. }
  9. // 抽象类中必须至少有一个抽象方法
  10. abstract public function get();
  11. }
  12. class demo extends iDemo{
  13. public $name;
  14. protected $age;
  15. private $country = '中国';
  16. public function get(){
  17. return $this->name . $this->age . $this->country;
  18. }
  19. public function __construct($a,$b)
  20. {
  21. $this->name = $a;
  22. $this->age = $b;
  23. }
  24. }
  25. class sun extends demo{
  26. public function __construct($a,$b,$c)
  27. {
  28. //子类 继承父类
  29. parent::__construct($a,$b);
  30. $this->c = $c;
  31. }
  32. //这里info()可以和父类get(),名字一致
  33. public function info()
  34. {
  35. return parent::get() . $this->c . ' 这是子类调用';
  36. }
  37. }
  38. $arr = new demo('张明朗',1);
  39. $arr->hello();
  40. $str = new sun('张明轩',3,'love');
  41. echo $str->info();
  1. <?php
  2. class main{
  3. function __set($_name,$_val){
  4. $this->$_name=$_val;//当外面直接设置私有成员属性$name的时候被自动调用
  5. }
  6. function __get($_name){
  7. return $this->$_name;//当外面直接使用私有成员属性$name的时候被自动调用
  8. }
  9. }
  10. $my = new main();
  11. $my->name="李四";//此时,就调用了__set()魔术方法
  12. echo $my->name;//此时,就调用__get()魔术方法

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