国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 php面向對象全攻略 (一) 面向對象基礎知識_php基礎

php面向對象全攻略 (一) 面向對象基礎知識_php基礎

May 17, 2016 am 09:02 AM
object-oriented

1.面向對象的概念
面向對象編程(Object Oriented Programming,OOP,面向對象程序設計)是一種計算機編程架構,OOP 的一條基本原則是計算機程序是由單個能夠起到子程序作用的單元或對象組合而成,OOP 達到了軟件工程的三個目標:重用性、靈活性和擴展性。為了實現整體運算,每個對象都能夠接收信息、處理數據和向其它對象發(fā)送信息。面向對象一直是軟件開發(fā)領域內比較熱門的話題,首先,面向對象符合人類看待事物的一般規(guī)律。其次,采用面向對象方法可以使系統(tǒng)各部分各司其職、各盡所能。為編程人員敞開了一扇大門,使其編程的代碼更簡潔、更易于維護,并且具有更強的可重用性。有人說PHP 不是一個真正的面向對象的語言,
這是事實。PHP 是一個混合型語言,你可以使用OOP,也可以使用傳統(tǒng)的過程化編程。然而,對于大型項目,你可能需要在PHP 中使用純的OOP 去聲明類,而且在你的項目里只用對象
和類。這個概念我先不多說了,因為有很多朋友遠離面向對象編程的主要原因就是一接觸面向對象概念的時候就理解不上去,所以就不想去學下去了。等讀者看完整篇內容后再去把概
念搞明白吧。
2.什么是類,什么是對象,類和對象之間的關系
類的概念:類是具有相同屬性和服務的一組對象的集合。它為屬于該類的所有對象提供了統(tǒng)一的抽象描述,其內部包括屬性和服務兩個主要部分。在面向對象的編程語言中,類是
一個獨立的程序單位,它應該有一個類名并包括屬性說明和服務說明兩個主要部分。
對象的概念:對象是系統(tǒng)中用來描述客觀事物的一個實體,它是構成系統(tǒng)的一個基本單位。一個對象由一組屬性和對這組屬性進行操作的一組服務組成。從更抽象的角度來說,對
象是問題域或實現域中某些事物的一個抽象,它反映該事物在系統(tǒng)中需要保存的信息和發(fā)揮的作用;它是一組屬性和有權對這些屬性進行操作的一組服務的封裝體??陀^世界是由對象
和對象之間的聯(lián)系組成的。
類與對象的關系就如模具和鑄件的關系,類的實例化結果就是對象,而對一類對象的抽象就是類。類描述了一組有相同特性(屬性)和相同行為(方法)的對象。
上面大概就是它們的定義吧,也許你是剛接觸面向對象的朋友,不要被概念的東西搞暈了,給你舉個例子吧,如果你去中關村想買幾臺組裝的PC 機,到了那里你第一步要干什么,
是不是裝機的工程師和你坐在一起,按你提供的信息和你一起完成一個裝機的配置單呀,這個配置單就可以想象成是類,它就是一張紙,但是它上面記錄了你要買的PC 機的信息,如
果用這個配置單買10 臺機器,那么這10 臺機子,都是按這個配置單組成的,所以說這10 臺機子是一個類型的,也可以說是一類的。那么什么是對象呢,類的實例化結果就是對象,用這
個配置單配置出來(實例化出來)的機子就是對象,是我們可以操作的實體,10 臺機子,10個對象。每臺機子都是獨立的,只能說明他們是同一類的,對其中一個機做任何動作都不會
影響其它9 臺機器,但是我對類修改,也就是在這個配置單上加一個或少一個配件,那么裝出來的9 個機子都改變了,這是類和對象的關系(類的實例化結果就是對象)。
3.什么是面向對象編程呢?
就不說他的概念,如果你想建立一個電腦教室,首先要有一個房間,房間里面要有N 臺電腦,有N 張桌子,N 把椅子,白板,投影機等等,這些是什么,剛才咱們說了,這就是對象,能看到的一個個的實體,可以說這個電腦教室的單位就是這一個個的實體對象,它們共
同組成了這個電腦教室,那么我們是做程序,這和面向對象有什么關系呢?開發(fā)一個系統(tǒng)程序和建一個電腦教室類似,你把每個獨立的功能模塊抽象成類,形成對象,由多個對象組成
這個系統(tǒng),這些對象之間都能夠接收信息、處理數據和向其它對象發(fā)送信息等等相互作用。
4.如何抽象出一個類?
上面已經介紹過了,面向對象程序的單位就是對象,但對象又是通過類的實例化出來的,所以我們首先要做的就是如何來聲明類,做出來一個類很容易,只要掌握基本的程序語法定
義規(guī)則就可以做的出來,那么難點在那里呢?一個項目要用到多少個類,用多少個對象,在那要定義類,定義一個什么樣的類,這個類實例化出多少個對象,類里面有多少個屬性,有
多少個方法等等,這就需要讀者通過在實際的開發(fā)中就實際問題分析設計和總結了。
類的定義:
class 類名{
}
使用一個關鍵字class 和后面加上一個你想要的類名以及加上一對大括號,這樣一個類的結構就定義出來了,只要在里面寫代碼就可以了,但是里面寫什么?能寫什么?怎樣寫才是
一個完整的類呢?上面講過來,使用類是為了讓它實例出對象來給我們用,這就要知道你想要的是什么樣的對象了,像上面我們講的一個裝機配置單上寫什么,你裝出來的機子就有什
么。比如說,一個人就是一個對象,你怎么把一個你看好的人推薦給你們領導呢?當然是越詳細越好了:
首先,你會介紹這個人姓名、性別、年齡、身高、體重、電話、家庭住址等等。
然后,你要介紹這個人能做什么,可以開車,會說英語,可以使用電腦等等。只要你介紹多一點,別人對這個人就多一點了解,這就是我們對一個人的描述,現在我
們總結一下,所有的對象我們用類去描述都是類似的,從上面人的描述可以看到,做出一個類來,從定義的角度分兩部分,第一是從靜態(tài)上描述,第二是從動態(tài)上描述,靜態(tài)上的描述
就是我們所說的屬性,像上面我們看到的,人的姓名、性別、年齡、身高、體重、電話、家庭住址等等。
動態(tài)上也就是人的這個對象的功能,比如這個人可以開車,會說英語,可以使用電腦等等,抽象成程序時,我們把動態(tài)的寫成函數或者說是方法,函數和方法是一樣的。所以,所有類
都是從屬性和方法這兩方面去寫,屬性又叫做這個類的成員屬性,方法叫做這個類的成員方法。
class 人{
成員屬性:姓名、性別、年齡、身高、體重、電話、家庭住址
成員方法:可以開車,會說英語,可以使用電腦
}
屬性:
通過在類定義中使用關鍵字"var"來聲明變量,即創(chuàng)建了類的屬性,雖然在聲明成員屬性的時候可以給定初始值,但是在聲明類的時候給成員屬性初始值是沒有必要的,比如說要是把
人的姓名賦上“張三”,那么用這個類實例出幾十個人,這幾十個人都叫張三了,所以沒有必
要,我們在實例出對象后給成員屬性初始值就可以了。
如:var $somevar;
方法(成員函數):
通過在類定義中聲明函數,即創(chuàng)建了類的方法。
如:function somefun(參數列表)
{ ... ... }
代碼片段
復制代碼 代碼如下:

class Person{
//下面是人的成員屬性
var $name; //人的名字
var $sex; //人的性別
var $age; //人的年齡
//下面是人的成員方法
function say(){
//這個人可以說話的方法
echo "這個人在說話";
} function run(){
//這個人可以走路的方法
echo "這個人在走路";
}
}
?>

上面就是一個類的聲明,從屬性和方法上聲明出來的一個類,但是成員屬性最好在聲明
的時候不要給初始的值,因為我們做的人這個類是一個描述信息,將來用它實例化對象,比
如實例化出來10 個人對象,那么這10 個人,每一個人的名字、性別、年齡都是不一樣的,
所以最好不要在這個地方給成員屬性賦初值,而是對每個對象分別賦值的。
用同樣的辦法可以做出你想要的類了,只要你能用屬性和方法能描述出來的實體都可以
定義成類,去實例化對象。
為了加強你對類的理解,我們再做一個類,做一個形狀的類,形狀的范圍廣了點,我們
就做個矩形吧,先分析一下,想一想從兩方面分析,矩形的屬性都有什么?矩形的功能都有
什么?
復制代碼 代碼如下:

class 矩形
{
//矩形的屬性
矩形的長;
矩形的寬;
//矩形的方法
矩形的周長;
矩形的面積;
}

代碼片段
復制代碼 代碼如下:

class Rect{
var $kuan;
var $gao;
function zhouChang(){
計算矩形的周長;
} function mianJi(){
計算矩形的面積;
}
}
?>

如果用這個類來創(chuàng)建出多個矩形對象,每個矩形對象都有自己的長和寬,都可以求出自
己的周長和面積了。
類的聲明我們就到這里吧?。?
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement object-oriented event-driven programming using Go language How to implement object-oriented event-driven programming using Go language Jul 20, 2023 pm 10:36 PM

How to use Go language to implement object-oriented event-driven programming Introduction: The object-oriented programming paradigm is widely used in software development, and event-driven programming is a common programming model that realizes the program flow through the triggering and processing of events. control. This article will introduce how to implement object-oriented event-driven programming using Go language and provide code examples. 1. The concept of event-driven programming Event-driven programming is a programming model based on events and messages, which transfers the flow control of the program to the triggering and processing of events. in event driven

What is the importance of @JsonIdentityInfo annotation using Jackson in Java? What is the importance of @JsonIdentityInfo annotation using Jackson in Java? Sep 23, 2023 am 09:37 AM

The @JsonIdentityInfo annotation is used when an object has a parent-child relationship in the Jackson library. The @JsonIdentityInfo annotation is used to indicate object identity during serialization and deserialization. ObjectIdGenerators.PropertyGenerator is an abstract placeholder class used to represent situations where the object identifier to be used comes from a POJO property. Syntax@Target(value={ANNOTATION_TYPE,TYPE,FIELD,METHOD,PARAMETER})@Retention(value=RUNTIME)public

PHP Advanced Features: Best Practices in Object-Oriented Programming PHP Advanced Features: Best Practices in Object-Oriented Programming Jun 05, 2024 pm 09:39 PM

OOP best practices in PHP include naming conventions, interfaces and abstract classes, inheritance and polymorphism, and dependency injection. Practical cases include: using warehouse mode to manage data and using strategy mode to implement sorting.

Analysis of object-oriented features of Go language Analysis of object-oriented features of Go language Apr 04, 2024 am 11:18 AM

The Go language supports object-oriented programming, defining objects through structs, defining methods using pointer receivers, and implementing polymorphism through interfaces. The object-oriented features provide code reuse, maintainability and encapsulation in the Go language, but there are also limitations such as the lack of traditional concepts of classes and inheritance and method signature casts.

Explore object-oriented programming in Go Explore object-oriented programming in Go Apr 04, 2024 am 10:39 AM

Go language supports object-oriented programming through type definition and method association. It does not support traditional inheritance, but is implemented through composition. Interfaces provide consistency between types and allow abstract methods to be defined. Practical cases show how to use OOP to manage customer information, including creating, obtaining, updating and deleting customer operations.

Are there any class-like object-oriented features in Golang? Are there any class-like object-oriented features in Golang? Mar 19, 2024 pm 02:51 PM

There is no concept of a class in the traditional sense in Golang (Go language), but it provides a data type called a structure, through which object-oriented features similar to classes can be achieved. In this article, we'll explain how to use structures to implement object-oriented features and provide concrete code examples. Definition and use of structures First, let's take a look at the definition and use of structures. In Golang, structures can be defined through the type keyword and then used where needed. Structures can contain attributes

Analyzing the Flyweight Pattern in PHP Object-Oriented Programming Analyzing the Flyweight Pattern in PHP Object-Oriented Programming Aug 14, 2023 pm 05:25 PM

Analyzing the Flyweight Pattern in PHP Object-Oriented Programming In object-oriented programming, design pattern is a commonly used software design method, which can improve the readability, maintainability and scalability of the code. Flyweight pattern is one of the design patterns that reduces memory overhead by sharing objects. This article will explore how to use flyweight mode in PHP to improve program performance. What is flyweight mode? Flyweight pattern is a structural design pattern whose purpose is to share the same object between different objects.

In-depth understanding of PHP object-oriented programming: Debugging techniques for object-oriented programming In-depth understanding of PHP object-oriented programming: Debugging techniques for object-oriented programming Jun 05, 2024 pm 08:50 PM

By mastering tracking object status, setting breakpoints, tracking exceptions and utilizing the xdebug extension, you can effectively debug PHP object-oriented programming code. 1. Track object status: Use var_dump() and print_r() to view object attributes and method values. 2. Set a breakpoint: Set a breakpoint in the development environment, and the debugger will pause when execution reaches the breakpoint, making it easier to check the object status. 3. Trace exceptions: Use try-catch blocks and getTraceAsString() to get the stack trace and message when the exception occurs. 4. Use the debugger: The xdebug_var_dump() function can inspect the contents of variables during code execution.

See all articles