Blogger Information
Blog 33
fans 0
comment 0
visits 19511
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月3日作业--php培训九期线上班
取个名字真难
Original
513 people have browsed it

课堂案例

  1. <?php
  2. // OOP基本步骤
  3. // 1. 创建类
  4. class demo1
  5. {
  6. // 2.添加类成员
  7. //创建属性
  8. public $site='php中文网';
  9. //创建方法
  10. public function getSite()
  11. {
  12. // 类的实例化
  13. $obj=new demo1();
  14. //返回类属性
  15. return $obj->site.'欢迎你';
  16. }
  17. }
  18. //3.访问类成员
  19. $obj=new demo1();
  20. //访问属性:对象成员访问符:->
  21. echo $obj->site.'<br>';
  22. echo $obj->getSite();
  23. echo '<h1>demo2.php</h1>';
  24. echo '<hr>';
  25. //1创建类
  26. class demo2
  27. {
  28. // 2.添加类成员
  29. public $site='php中文网';
  30. public $role='讲师';
  31. // 创建方法
  32. public function getSite()
  33. {
  34. // 类的实例化
  35. $obj1=new self;
  36. //返回类的属性
  37. return $this->site.'欢迎你';
  38. }
  39. public function getRole()
  40. {
  41. // 类的实例化
  42. $obj1=new self;
  43. // 返回属性
  44. //$this是当前类的实例引用它与当前类的实例绑定
  45. return $this->role;
  46. }
  47. }
  48. //3.访问类成员
  49. $obj1=new demo2();
  50. //访问属性:对象成员访问符:->
  51. echo $obj1->site.'<br>';
  52. echo $obj1->getRole().'<br>';
  53. echo '<h1>demo3.php</h1>';
  54. echo '<hr>';
  55. //1创建类
  56. class demo3
  57. {
  58. // 2添成类成员
  59. public $site3;
  60. public $role3;
  61. public function getInfo()
  62. {
  63. return'我是:'.$this->site3.$this->role3;
  64. }
  65. //构造方法
  66. public function __construct($site3,$role3)
  67. {
  68. // 初始化类成员
  69. $this->site3=$site3;
  70. $this->role3=$role3;
  71. // 创建类实例
  72. //$obj3=new self;
  73. ////3.添加类实例成员
  74. // $obj3->site3=$this->site3;
  75. // $obj3->role3=$role3;
  76. //// 返回实例
  77. // return $obj3;
  78. }
  79. }
  80. //3访问类成员
  81. $obj3=new demo3('php中文网',lectrue);
  82. echo $obj3->getInfo('php中文网',lectrue);
  83. echo '<h1>demo4.php</h1>';
  84. echo '<hr>';
  85. //1.创建类
  86. class demo4
  87. {
  88. // 2.添成类成员
  89. public $site4;
  90. // 如果不想让该属性在类的外部被访问,可以将该属性的访问控制符修改为:private 私有成员, protected 受保护成员
  91. private $name='peter zhu';
  92. protected $role4;
  93. public function getInfo()
  94. {
  95. return '我是:'.$this->site4.$this->role4;
  96. }
  97. //构造方法
  98. public function __construct($site4,$role4)
  99. {
  100. $this->site4=$site4;
  101. $this->role4=$role4;
  102. }
  103. public function getRole()
  104. {
  105. // 仅允许用户名是'admin'的用户访问,其它访问返回: 无权访问
  106. $username=$_GET['username'] ??'';
  107. if(isset($username)&& $username==='admin' )
  108. {
  109. return $this->role4;
  110. }else{
  111. return '无权访问';
  112. }
  113. }
  114. public function getName()
  115. {
  116. return isset($this->name)?$this->name:'不存在该属性';
  117. }
  118. // 魔术方法: __get($name), 属性重载
  119. // public function __get($name)
  120. // {
  121. // // 仅允许用户名是'admin'的用户访问,其它访问返回: 无权访问
  122. // $username=$_GET['username']??'';
  123. // if (isset($user)&&$username==='admin'){
  124. // return isset($this->name)?$this->name:'属性没有定义;'
  125. // }else{
  126. // return '无权';
  127. // }
  128. // }
  129. }
  130. //3.访问类成员
  131. $obj4=new demo4('wwww.php.cn','讲师');
  132. //echo $obj4->role4;
  133. echo $obj4->getRole();
  134. echo $obj4->getName();
  135. echo '<h1>demo5.php</h1>';
  136. echo '<hr>';
  137. // 类的继承
  138. //1.创建类
  139. class demo5
  140. {
  141. // 2.添加类成员
  142. public $site5;
  143. protected $role5;
  144. public function getInfo()
  145. {
  146. return'我是:'.$this->site5.$this->role5;
  147. }
  148. // 构造方法
  149. public function __construct($site5,$role5)
  150. {
  151. $this->site5=$site5;
  152. $this->role5=$role5;
  153. }
  154. }
  155. //子类
  156. class demo5_1 extends demo5
  157. {
  158. // 当前子类的自身属性
  159. private $course;
  160. public function __construct($site5, $role5,$course)
  161. {
  162. // parent:: 调用 父类中的成员
  163. parent::__construct($site5, $role5);
  164. $this->course=$course;
  165. }
  166. // 重写父类中的方法
  167. public function getInfo()
  168. {
  169. return parent::getInfo().'负责的课程是:'.$this->course;
  170. }
  171. }
  172. //3.访问类成员
  173. $sub=new demo5_1('php.cn', '朱老师', 'php');
  174. echo $sub->getInfo();
  175. echo '<h1>demo6.php</h1>';
  176. echo '<hr>';
  177. //trait的使用
  178. trait Test
  179. {
  180. public function getInfo()
  181. {
  182. return'我是:'.$this->site6.'讲师'.$this->role6;
  183. }
  184. }
  185. //1.创建类宿主类
  186. class demo6
  187. {
  188. //导入trait类库
  189. use Test;
  190. //2.添成类成员
  191. public $site6;
  192. protected $role6;
  193. public function getInfo()
  194. {
  195. return'我是:'.$this->site6.'讲师'.$this->role6;
  196. }
  197. //构造方法
  198. public function __construct($site6,$role6)
  199. {
  200. $this->role6=$role6;
  201. $this->site6=$site6;
  202. }
  203. }
  204. // trait : 还可以用在类的继承上下文环境中
  205. class demo6_1 extends demo6
  206. {
  207. // 当前子类的属性
  208. private $course;
  209. public function __construct($site6, $role6, $course)
  210. {
  211. parent::__construct($site6, $role6);
  212. $this->course=$course;
  213. }
  214. // 重写父类中的方法
  215. public function getInfo()
  216. {
  217. return parent::getInfo() . ', 负责的课程是: ' . $this->course;
  218. }
  219. }
  220. // 优先级: 当前类中的同名方法 > trait类中的同名方法 > 父类中的同名方法
  221. //3.访问类成员
  222. $obj = new Demo6('php中文网', 'Peter Zhu');
  223. echo $obj->getInfo();
  224. echo '<hr>';
  225. $sub = new Demo6_1('php.cn', '朱老师', 'php');
  226. echo $sub->getInfo();
  227. echo '<h1>demo7.php</h1>';
  228. echo '<hr>';
  229. //接口
  230. interface iDemo
  231. {
  232. // 接口方法
  233. public function getInfo();
  234. public function hello();
  235. }
  236. //1.创建类
  237. class Demo7 implements iDemo
  238. {
  239. // 2.添加类成员
  240. public $site7;
  241. protected $role7;
  242. public function getInfo()
  243. {
  244. return'我是:'.$this->site7.'讲师'.$this->role7;
  245. }
  246. public function hello()
  247. {
  248. return 'Hello 大家晚上好?';
  249. }
  250. // 构造方法
  251. public function __construct($site, $role)
  252. {
  253. $this->site = $site;
  254. $this->role = $role;
  255. }
  256. }
  257. // 3. 访问类成员
  258. $obj = new Demo7('php中文网', '猪老师');
  259. echo $obj->getInfo() . '<br>';
  260. echo $obj->hello() . '<br>';
  261. echo '<h1>demo8.php</h1>';
  262. echo '<hr>';
  263. // 抽象类
  264. //创建抽象类
  265. abstract class cx
  266. {
  267. // 抽象方法
  268. abstract public function getInfo();
  269. // 已实现方法
  270. public function hello()
  271. {
  272. return'hello大家晚上好';
  273. }
  274. }
  275. //1.创建类
  276. class demo8 extends cx
  277. {
  278. // 2.添加类成员
  279. public $site8;
  280. protected $role8;
  281. public function getInfo()
  282. {
  283. return '我是: ' . $this->site . '讲师: ' . $this->role;
  284. }
  285. // 构造方法
  286. public function __construct($site, $role)
  287. {
  288. $this->site8 = $site;
  289. $this->role8 = $role;
  290. }
  291. }
  292. // 3. 访问类成员
  293. $obj = new Demo8('php中文网', '猪哥');
  294. echo $obj->getInfo() . '<br>';
  295. echo $obj->hello() . '<br>';


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!