Blogger Information
Blog 29
fans 0
comment 0
visits 14204
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
面向对象编程学习小结(一)
P粉317509817
Original
424 people have browsed it

类的属性和方法

代码:

  1. <?php
  2. // 类声明
  3. class Person
  4. {
  5. private $name;
  6. private $age;
  7. // 静态属性
  8. private static $number = 0;
  9. // 自定义方法
  10. public function setClass($name,$age){
  11. $this->name = $name;
  12. $this->age = $age;
  13. }
  14. // 魔术方法
  15. public function __construct(string $name,int $age)
  16. {
  17. $this->name = $name;
  18. $this->age = $age;
  19. self::$number++;
  20. }
  21. // 自定义方法:显示函数
  22. public function Print(){
  23. echo '姓名: '.$this->name.' 年龄:'.$this->age;
  24. }
  25. // 静态方法
  26. public static function PeoPrint(){
  27. echo '<br>'.'记录人数:'.self::$number.'<br>';
  28. }
  29. }
  30. // 创建一个人类
  31. $people1=new Person('xyz',20);
  32. $people1->Print();
  33. // 查看人数
  34. Person::PeoPrint();
  35. // 修改年龄和姓名;
  36. $people1->setClass('111',10);
  37. $people1->Print();
  38. Person::PeoPrint();
  39. // 多创建几个
  40. $people2= new Person('222',1);
  41. $people3= new Person('333',11);
  42. $people4= new Person('444',13);
  43. $people5= new Person('555',14);
  44. // 查看记录人数:
  45. Person::PeoPrint();

效果:

类的扩展/抽象/最终

  1. <?php
  2. // 定义一个抽象父类
  3. abstract class Person{
  4. // protect能被继承
  5. protected $name;
  6. protected $age;
  7. protected $gender;
  8. // 构造函数
  9. public function __construct(string $name,int $age,string $gender)
  10. {
  11. $this->name=$name;
  12. $this->age=$age;
  13. $this->gender=$gender;
  14. }
  15. // 打印函数
  16. public function PPrint(){
  17. echo '姓名: '.$this->name.'<br>'.'年龄: '.$this->age.'<br>'.'性别: '.$this->gender.'<br>';
  18. }
  19. // 抽象方法: 只有方法名,参数列表,没有具体实现(大括号)
  20. // 抽象方法被继承时,一定要重写
  21. abstract protected function hr();
  22. }
  23. // 学生类,定义为最终类,防止继承
  24. final class Student extends Person
  25. {
  26. private $class;
  27. private $score;
  28. // 构造函数
  29. public function __construct(string $name,int $age,string $gender,string $class,int $score){
  30. parent::__construct($name,$age,$gender);
  31. $this->class = $class;
  32. $this->score = $score;
  33. }
  34. public function PPrint()
  35. {
  36. parent::PPrint();
  37. echo '<br>'.'课程: '.$this->class.'<br>'.'分数: '.$this->score.'<br>';
  38. }
  39. public function hr(){
  40. echo '<hr>';
  41. }
  42. }
  43. $student1=new Student('xyz',12,'男','数学',120);
  44. $student1->PPrint();
  45. $student1->hr();

效果:

接口

  1. <?php
  2. // 接口: 大号的抽象类
  3. // 接口的所有成员,必须是抽象
  4. // interface: 声明接口
  5. // implements: 实现接口
  6. interface iPerson{
  7. const Nation='CHINA';
  8. // 必须是抽象,必须是public
  9. public function PPrint();
  10. public function hr();
  11. }
  12. abstract class Person implements iPerson{
  13. // protect能被继承
  14. protected $name;
  15. protected $age;
  16. protected $gender;
  17. // 构造函数
  18. public function __construct(string $name,int $age,string $gender)
  19. {
  20. $this->name=$name;
  21. $this->age=$age;
  22. $this->gender=$gender;
  23. }
  24. // 打印函数
  25. public function PPrint(){
  26. echo '姓名: '.$this->name.'<br>'.'年龄: '.$this->age.'<br>'.'性别: '.$this->gender.'<br>';
  27. }
  28. }
  29. final class Student extends Person
  30. {
  31. private $class;
  32. private $score;
  33. // 构造函数
  34. public function __construct(string $name,int $age,string $gender,string $class,int $score){
  35. parent::__construct($name,$age,$gender);
  36. $this->class = $class;
  37. $this->score = $score;
  38. }
  39. public function PPrint()
  40. {
  41. parent::PPrint();
  42. echo '<br>'.'课程: '.$this->class.'<br>'.'分数: '.$this->score.'<br>';
  43. }
  44. public function hr(){
  45. echo '<hr>';
  46. }
  47. }
  48. $student1=new Student('xyz',12,'男','数学',100);
  49. $student1->PPrint();
  50. $student1->hr();

效果:

Correcting teacher:PHPzPHPz

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