每泓清水都是远古恐龙的尿 發表於 2019-6-17 00:02:00

PHP面向对象

<p><span style="font-size: 18px"><strong>1.面向对象介绍</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 面向对象是一个编程思想。编程思想有面向过程和面向对象</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 面向过程:编程思路集中的是过程上</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 面向对象:编程思路集中在参与的对象</strong></span></p>
<p><span style="font-size: 18px"><strong>2.面向对象的好处</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1.多人合作方便</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2.减少代码的冗余,灵活性高</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3.代码的可重用性发挥到极致</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4.可扩展性强</strong></span></p>
<p><span style="font-size: 18px"><strong>3.类和对象</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.对象是具体存在的事物,对象是由属性和方法组成</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2.类是具有相同属性和行为的一组对象的集合</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;注意:一个类可以创建多个对象</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;小结:</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.对象是由属性和方法组成的</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2.类是所有对象的相同属性和方法的集合</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3.在开发的时候先写类,通过类创建对象,通过对象调用方法和属性</strong></span></p>
<p><span style="font-size: 18px"><strong>4.在PHP中实现类和对象</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;4.1创建类</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> 类名{
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">属性
    //方法
    //常量</span>
<span style="color: rgba(0, 0, 0, 1)">}
类是由属性、方法、常量组成的,也可以说
类成员有:属性、方法、常量 </span></pre>
</div>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="font-size: 18px"><strong>类名的命名规则:</strong></span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="font-size: 18px"><strong>1.以字母、下划线开头,后面跟的是字母、数字、下划线</strong></span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="font-size: 18px"><strong>2.不能用PHP关键字做类名</strong></span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-size: 18px"><strong>3.类名不区分大小写(变量名区分,关键字、类名不区分大小写)</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4.类名用帕斯卡命名法(大驼峰&nbsp; 单词的首字母大写)</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4.2对象实例化</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 通过new关键字来实例化对象</strong></span></p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义类</span>
<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Student {
   
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">实例化对象</span>
<span style="color: rgba(128, 0, 128, 1)">$stu1</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> Student();</pre>
</div>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-size: 18px"><strong>&nbsp; 4.3对象的比较</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;注意:对象的传递是地址传递</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;相等:结构和保存的值一样就相等</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;全等:指向同一个对象才是全等&nbsp;</strong></span></p>
<p><span style="font-size: 18px"><strong>5.属性</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;属性本质就是变量</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;通过 -&gt;调用对象的成员&nbsp; &nbsp;对象名-&gt;属性名&nbsp; &nbsp; 对象名-&gt;方法名()<br></strong></span></p>
<p><span style="font-size: 18px"><strong>6.方法</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;方法的本质就是函数</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;小结:</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.方法前面public是可以省略的,如果省略,默认就是public的。</strong></span></p>
<p><span style="font-size: 18px"><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2.属性前面的public不能省略</strong></span></p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/shineguang/p/11037535.html
頁: [1]
查看完整版本: PHP面向对象