Blogger Information
Blog 20
fans 0
comment 0
visits 10794
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php面向对象的入门学习(类与对象)
麦兜的故事
Original
483 people have browsed it

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

  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. ?>
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