Blogger Information
Blog 34
fans 2
comment 0
visits 23180
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月27号作业,类和对像
遗忘了寂寞
Original
497 people have browsed it

命名空间namespace 创建类 class

  1. <?php
  2. //命名空间
  3. namespace my\name;
  4. //创建类
  5. class a{
  6. public $name = '张三';
  7. public $age =18;
  8. public function get(){
  9. echo $this->name . '在吃饭';
  10. }
  11. }
  12. //实例化类:
  13. $obj= new \my\name\a;
  14. $obj->get();

成员变量、成员方法

  1. <?php
  2. // 成员变量、成员方法
  3. class spObj{
  4. public $name;
  5. public $height;
  6. public $avo;
  7. public function bootFootBall($name,$height,$avo){
  8. $this->name=$name;
  9. $this->height=$height;
  10. $this->avo=$avo;
  11. if($this->height<185 and $this->avo <85){
  12. return $this->name .';符合踢足球的要求!';
  13. }else{
  14. return $this->name .';不符合踢足球的要求!';
  15. }
  16. }
  17. }
  18. $obj=new spObj();
  19. echo $obj->bootFootBall('张三','185','80');

构造方法

  1. <?php
  2. // 构造方法
  3. class spObj{
  4. public $name;
  5. public $height;
  6. public function __construct($name,$height){
  7. $this->name=$name;
  8. $this->height=$height;
  9. }
  10. public function shuchu(){
  11. echo $this->name . '的身高是' . $this->height ;
  12. }
  13. }
  14. $spObj=new spObj('张三','185');
  15. $spObj->shuchu();



Correcting teacher:查无此人查无此人

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