类的属性和方法

代码:

  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();

效果:

更多相关文章

  1. 【Android】SerialPortFinder学习笔记,显示串口列表
  2. Android—React Native编程
  3. 最全面总结 Android(安卓)WebView与 JS 的交互方式
  4. Android(安卓)反编译apk 到java源码的方法
  5. android Kotlin 继承、派生、接口、构造方式,方法、属性重写
  6. Android开发知识(八):Android事件处理机制:事件分发、传递、拦截、处
  7. android SDK manager 提取慢或者下载慢解决方法
  8. Android(安卓)反编译apk 到java源码的方法
  9. android switch模块 (耳机检测)

随机推荐

  1. android app模拟 persistent 属性可以保
  2. android ndk 开发之 在 应用程序中使用 j
  3. [置顶] android调用第三方库——第二篇—
  4. Relativelayout的一些属性
  5. Android摄像头照相机技术-android学习之
  6. Android XMl文件中tools前缀
  7. android Textview过长时显示省略号
  8. Android程序反编译的方法[已更新]
  9. Android 内存管理机制
  10. Android API课程1: Application Fundament