Blogger Information
Blog 52
fans 1
comment 1
visits 38280
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
作业内容:oop基础
小丑0o鱼
Original
400 people have browsed it
  1. 1. 类(对象抽象化的结果)与对象 (类实例化结果)
  2. 1、对象是具体存在的事物,对象是由属性(变量)和方法(函数)组成的
  3. 2、类是具有相同属性和行为的一组对象的集合
  4. 创建类
  5. 语法:
  6. class 类名{ //属性 //方法 //常量 }
  7. 类是由属性、方法、常量组成的,也可以说
  8. 类成员有:属性、方法、常量
  9. 类名的命名规则:
  10. 以字母、下划线开头,后面跟的是字母、数字、下划线
  11. 不能用PHP关键字做类名
  12. 类名不区分大小写(变量名区分,关键字、类名不区分大小写)
  13. 类名用帕斯卡命名法(大驼峰 单词的首字母大写
  14. 对象实例化
  15. 通过new关键字来实例化对象。
  16. 2. 构造方法
  17. 构造方法也叫构造函数,当实例化对象的时候自动执行。 语法:
  18. function __construct(){ }
  19. 注意:前面是两个下划线
  20. 构造函数作用:初始化成员变量
  21. 注意:构造函数可以带参数,但不能有return
  22. class Student
  23. {
  24. private $name;
  25. private $sex;
  26. //构造函数初始化成员变量
  27. public function __construct($name,$sex)
  28. {
  29. $this->name=$name;
  30. $this->sex=$sex;
  31. }
  32. //显示信息
  33. public function show()
  34. {
  35. echo "姓名:{$this->name}<br>";
  36. echo "性别:{$this->sex}<br>";
  37. }
  38. }
  39. //实例化
  40. $stu=new Student('tom','男');
  41. $stu->show(); //输出 姓名:tom 性别:男
  42. 3. 对象成员之间的内部访问 $this
  43. $this表示当前对象的引用,也就是$this保存的当前对象的地址
  44. class A
  45. {
  46. public $sttr;
  47. public function getSttr()
  48. {
  49. return $this->sttr;
  50. }
  51. }
  52. $a = new A();
  53. $a->sttr = '属性';
  54. echo $a->getSttr(); //输出 属性
  55. 4. private仅限本类中使用 protected本类中,子类中使用
  56. 访问修饰符
  57. 用来控制成员的访问权限
  58. 修饰符 描述
  59. public(公有的) 在类的内部和外部都能访问
  60. private(私有的) 只能在类的内部访问
  61. protected(受保护的) 在整个继承链上访问
  62. 一般来说,属性都用私有的,通过公有的方法对私有的属性进行赋值和取值。
  63. class Animal
  64. {
  65. // 公有
  66. public $name;
  67. // 保护
  68. protected $type;
  69. // 私有
  70. private $dna;
  71. // 构造方法
  72. public function __construct($name,$type,$dna)
  73. {
  74. $this->name = $name;
  75. $this->type = $type;
  76. $this->dna = $dna;
  77. }
  78. // 公有方法对私有属性取值
  79. public function getDna()
  80. {
  81. return $this->dna;
  82. }
  83. }
  84. // 子类extend继承
  85. class Dog extends Animal
  86. { // 子类未定义构造方法,默认调用父类的
  87. // 子类通过公有方法对保护属性取值
  88. public function getType()
  89. {
  90. return $this->type;
  91. }
  92. public function getDna()
  93. {
  94. parent::getDna();
  95. }
  96. }
  97. // 子类对象
  98. $husky = new Dog('哈士奇','husky','002001');
  99. echo $husky->name; //输出 '哈士奇'
  100. //echo $husky->type; //错误
  101. //echo $husky->dna; //错误
  102. echo $husky->getDna(); // 无输出,子类拿不到父类的私有
  103. echo $husky->getType(); // 输出 husky
  104. // 父类对象
  105. $cat = new Animal('mimi','cat','001');
  106. echo $cat->getDna(); // 输出002
  107. 5. 类的自动加载 spl_autoload_register
  108. 类的规则
  109. 一个文件中只能放一个类(必须)
  110. 文件名和类名同名(必须)
  111. 类文件以.class.php结尾(不是必须
  112. 手动加载类
  113. require ‘类文件路径’
  114. 自动加载类
  115. 当缺少类的时候自动的调用autoload()函数,并且将缺少的类名作为参数传递给autoload()。
  116. function __autoload($class_name)
  117. {
  118. require $class_name.'php';
  119. }
  120. 注册加载类
  121. 通过spl_autoload_register($callback)注册__autoload()函数
  122. spl_autoload_register(function ($class_name)
  123. {
  124. require $class_name.'.php';
  125. }
  126. );
  127. 可以注册多个自动加载函数
  128. spl_autoload_register('load1');
  129. spl_autoload_register('load2');
  130. spl_autoload_register('load3');
  131. function load1($className)
  132. {
  133. require $className.'.php';
  134. }
  135. function load2($className)
  136. {
  137. require $className.'.class.php';
  138. }
  139. function load3($className)
  140. {
  141. require $className.'.fun.php';
  142. }
  143. 6. 静态成员的访问 类的引用self::
  144. static修饰的属性叫静态属性、static修饰的方法叫静态方法
  145. 静态成员加载类的时候分配空间,程序执行完毕后销毁
  146. 静态成员在内存中就一份。
  147. 调用语法 类名::属性 类名::方法名()
  148. self表示所在类的类名,使用self降低耦合性
  149. class Cat
  150. {
  151. //静态属性
  152. static $type;
  153. //静态方法设置静态属性
  154. public static function setType($type)
  155. {
  156. self::$type = $type;
  157. }
  158. //静态方法获得静态属性
  159. public static function getType()
  160. {
  161. return self::$type;
  162. }
  163. }
  164. Cat::$type = '波斯猫';
  165. echo Cat::$type; //输出 波斯猫
  166. Cat::setType('招财猫');
  167. echo Cat::getType(); //输出 招财猫
  168. 7. 类的继承 扩展 父类方法(魔术方法,普通方法)的重写 parent:: 调用父类成员
  169. 继承介绍
  170. 继承使得代码具有层次结构
  171. 子类继承了父类的属性和方法,实现了代码的可重用性。
  172. 使用extends关键字实现继承
  173. 父类和子类是相对的
  174. 语法 :
  175. class 子类 extends 父类{ }
  176. 子类中调用父类成员
  177. 1、方法一:通过实例化父类调用父类的成员
  178. 2、方法二:通过$this关键字调用父类的成员
  179. protected
  180. protected:受保护的,在整个继承链上使用
  181. 继承中的构造函数
  182. 规则:
  183. 1、如果子类有构造函数就调用子类的,如果子类没有就调用父类的构造函数。
  184. 2、子类的构造函数调用后,默认不再调用父类的构造函数
  185. 可通过parent调用父类的构造函数
  186. parent::__construct()
  187. $this详解
  188. $this表示当前对象的引用,也就是$this保存的当前对象的地址
  189. 多重继承
  190. PHP不允许多重继承,因为多重继承容易产生二义性
  191. 如何实现C继承AB,使用继承链
  192. // 父类
  193. class Human
  194. {
  195. public $name;
  196. public function __construct($name)
  197. {
  198. $this->name = $name;
  199. }
  200. public function say()
  201. {
  202. return $this->name.' say: hello!';
  203. }
  204. }
  205. // 子类
  206. class Chinese extends Human
  207. {
  208. private $nationality = 'CN';
  209. private $ethnicity;
  210. public function __construct($name,$ethnicity)
  211. {
  212. // 调用父类构造方法后, 加入自
  213. parent::__construct($name);
  214. $this->ethnicity = $ethnicity;
  215. }
  216. // 重写父类say方法
  217. public function say()
  218. {
  219. return parent::say() . "你好?";
  220. }
  221. // 获得属性
  222. public function getAttr()
  223. {
  224. return "国籍:{$this->nationality},民族:{$this->ethnicity}";
  225. }
  226. }
  227. // 实例化子类
  228. $people = new Chinese('张三', '汉');
  229. echo $people->name; //输出 张三
  230. echo $people->say(); //输出 张三say:hello!你好?
  231. echo $people->getAttr(); //输出 国籍:CN,民族:汉
Correction status:Uncorrected

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!