请实例演绎你对面向对象类与对象关系的理解?

  1. <?php
  2. // 类里面定义一系列的属性和操作的方法
  3. // 而对象就是要把属性进行实例化,然后交给类里面的方法去处理
  4. class Persons{
  5. public $name;
  6. public $age;
  7. public $height;
  8. public $weight;
  9. public function whoName(){
  10. echo $this->name.' is '.$this->age.' years old and '.$this->height.' m ';
  11. }
  12. }
  13. $student = new Persons;
  14. $student->name = 'Jack';
  15. $student->age = 20;
  16. $student->height = 1.80;
  17. $student->whoName();
  18. ?>

请实例演绎oop的封装性是怎么实现的?

  1. <?php
  2. // 类里面定义一系列的属性和操作的方法
  3. // 而对象就是要把属性进行实例化,然后交给类里面的方法去处理
  4. // 封装是隐藏对象中的属性或方法
  5. class Persons{
  6. public $name;
  7. public $age;
  8. public $height;
  9. // 受保护的,外部不能直接访问
  10. protected $weight=45;
  11. public function whoName(){
  12. // 把受保护的属性封装在方法里面,从而外部可以看到
  13. echo $this->name.' is '.$this->age.' years old and '.$this->height.' m <br/>';
  14. echo $this->name.' is '.$this->weight .'kg';
  15. }
  16. }
  17. $student = new Persons;
  18. $student->name = 'Jack';
  19. $student->age = 20;
  20. $student->height = 1.80;
  21. $student->whoName();
  22. ?>

请实例演绎构造函数的作用有哪些?

  1. <?php
  2. class Persons{
  3. public $name;
  4. public $age;
  5. public $height;
  6. // 受保护的,外部不能直接访问
  7. protected $weight=45;
  8. // _constructor 构造函数
  9. // 作用
  10. // 1. 初始化类成员 让类/实例化的状态稳定下来
  11. // 2. 给对象属性进行初始化赋值
  12. // 3. 可以给私有成员,受保护的成员初始化赋值
  13. public function __construct($name,$age,$height,$weight){
  14. $this->name = $name;
  15. $this->age = $age;
  16. $this->height = $height;
  17. $this->weight = $weight;
  18. }
  19. public function whoName(){
  20. // 把受保护的属性封装在方法里面,从而外部可以看到
  21. echo $this->name . ' is '.$this->age.' years old and '.$this->height.' m <br/>';
  22. echo $this->name . ' is '.$this->weight .'kg';
  23. }
  24. }
  25. $person = new Persons('Jack',18,1.85,80);
  26. echo $person->whoName();
  27. ?>

更多相关文章

  1. 1. 请实例演绎你对面向对象类与对象关系的理解? 2. 请实例演绎oop
  2. 面向对象编程(oop)初体验
  3. 类的属性、封装、构造函数
  4. php简单实现模拟用户登陆验证
  5. PHP数组处理初体验
  6. 回调函数与递归函数实例总结
  7. 实例演绎对回调函数与递归函数的理解?
  8. 返回数组中所有的值并给其建立从0开始递增的数字索引
  9. PHP函数的返回值与参数,匿名函数与变量作用域的总结与实例

随机推荐

  1. Python 中的数字到底是什么?
  2. 监控流媒体服务器连接监控摄像头的配置方
  3. 在剪贴板上读取/写入数据,太方便了吧!
  4. Python 为什么不支持 switch 语句?
  5. Python 疑难问题:[] 与 list() 哪个快?为什
  6. AWS上传证书-添加负载均衡
  7. 给你的Excel增加正则处理函数,简直如虎添
  8. 数据分析都有哪些岗位?
  9. 什么是好的数据指标:精益数据分析
  10. 为什么说 Python 内置函数并不是万能的?