Blogger Information
Blog 34
fans 0
comment 0
visits 20286
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php灭绝师太私密亲传面向对象(OOP)编程
小庄
Original
552 people have browsed it

php灭绝师太私密亲传面向对象(OOP)编程

  1. <?php
  2. class Person{
  3. public $Name;
  4. protected $Age;
  5. public $Job;
  6. //构造函数 一般用于初始化
  7. public function __construct($name,$age,$job){
  8. $this->Name = $name;
  9. $this->Age = $age;
  10. $this->Job = $job;
  11. }
  12. //对外访问方法,用于对外访问不到受保护的,及私有的属性
  13. public function getAge(){
  14. return $this->Age;
  15. }
  16. }
  17. //实例化Person 类
  18. $wang = new Person("王小明",24,"摸鱼的");
  19. $li = new Person("李大明",35,"凑数的");
  20. $chen = new Person("陈楚生",36,"歌手");
  21. $liu = new Person("刘德华",59,"演员");
  22. //Age属性是受保护的,出本类以及派生类外,都无法访问,但是可以用访问器进行访问
  23. // echo $wang->Age;
  24. echo $wang->Name;
  25. echo $wang->getAge();
  26. echo $wang->Job;
  27. echo "<hr/>";
  28. echo $li->Name;
  29. echo $li->getAge();
  30. echo $li->Job;
  31. echo "<hr/>";
  32. echo $chen->Name;
  33. echo $chen->getAge();
  34. echo $chen->Job;
  35. echo "<hr/>";
  36. echo $liu->Name;
  37. echo $liu->getAge();
  38. echo $liu->Job;
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!