在Go語言中,雖然沒有像面向?qū)ο笳Z言那樣的類,但通過結(jié)構(gòu)體類型和方法,仍然支持部分面向?qū)ο缶幊蹋∣OP)的概念。
封裝是一種將一個(gè)對(duì)象的實(shí)現(xiàn)細(xì)節(jié)隱藏起來,使其對(duì)其他對(duì)象不可見的做法,這樣可以實(shí)現(xiàn)解耦。
例如,考慮以下結(jié)構(gòu)體:
type Student struct{ name string rollNo uint64}
其中的name和rollNo字段是私有的,因?yàn)樗鼈円孕懽帜搁_頭。為了提供公共訪問,可以為這些字段定義對(duì)應(yīng)的getter和setter方法。
func (s *Student) GetName() string { return s.name}func (s *Student) SetName(name string) { s.name = name}func (s *Student) GetRollNo() uint64 { return s.rollNo}func (s *Student) SetRollNo(roll uint64) { s.rollNo = roll}
現(xiàn)在,程序的其他部分也可以創(chuàng)建Student結(jié)構(gòu)體的對(duì)象,并通過公共的getter和setter方法訪問name和rollNo,從而實(shí)現(xiàn)了封裝。
數(shù)據(jù)抽象是一種設(shè)計(jì)模式,其中數(shù)據(jù)僅對(duì)語義相關(guān)的函數(shù)可見,以防止誤用。數(shù)據(jù)抽象的成功導(dǎo)致在面向?qū)ο蠛图兒瘮?shù)式編程中頻繁地將數(shù)據(jù)隱藏作為設(shè)計(jì)原則。
在Go語言中,可以通過接口實(shí)現(xiàn)抽象。接口定義了一組必須實(shí)現(xiàn)的方法,以滿足接口的要求。
例如,考慮以下接口:
type iStudent interface{ calcPercentage() float64}
該接口定義了一個(gè)名為calcPercentage()的方法,用于計(jì)算學(xué)生的百分比。
在Go語言中,不支持傳統(tǒng)意義上的繼承。相反,Go使用組合來實(shí)現(xiàn)類似的功能。一個(gè)結(jié)構(gòu)體可以嵌入另一個(gè)結(jié)構(gòu)體,從而繼承其字段和方法。
例如,考慮以下結(jié)構(gòu)體:
type Person struct { name string age uint64}type Student struct { Person studentID uint64}
在這里,Student結(jié)構(gòu)體嵌入了Person結(jié)構(gòu)體,這意味著它繼承了Person的name字段。Student結(jié)構(gòu)體還有自己的studentID字段。現(xiàn)在,任何對(duì)Person操作的方法也可以對(duì)Student操作,因?yàn)镾tudent是一個(gè)帶有額外字段的Person。
多態(tài)是指調(diào)用代碼可以獨(dú)立于所支持層次結(jié)構(gòu)中的類(父類或其子類)而運(yùn)行的情況。
在Go語言中,可以通過使用接口來實(shí)現(xiàn)多態(tài)。由于Go是一種靜態(tài)類型的語言,變量的類型必須在編譯時(shí)已知。然而,通過使用接口,可以編寫可以在滿足相同接口要求的不同類型上操作的代碼,而無需在編譯時(shí)知道其具體類型。
例如,考慮以下代碼:
func PrintStudentDetails(s *Student) { fmt.Println("Student Name:", s.GetName()) fmt.Println("Student Age:", s.GetAge()) fmt.Println("Percentage",s.CalcPercentage())}
現(xiàn)在,我們可以將任何實(shí)現(xiàn)了PrintStudentDetails()方法的類型作為參數(shù)調(diào)用該函數(shù),比如Student類型,它會(huì)顯示該學(xué)生的詳細(xì)信息。這就是多態(tài)的實(shí)現(xiàn)。
完整的面向?qū)ο缶幊淘贕o中的示例:
package mainimport "fmt"type Shape interface { Area() float64}type Triangle struct { Base, Height float64}func (t Triangle) Area() float64 { return 0.5 * t.Base * t.Height}type Square struct { Side float64}func (s Square) Area() float64 { return s.Side * s.Side}type Rectangle struct { Length, Breadth float64}func (r Rectangle) Area() float64 { return r.Length * r.Breadth}func printArea(s Shape) { fmt.Println("Area of shape is : ", s.Area())}func main() { t := Triangle{Base: 10, Height: 20} printArea(t) //Area of shape is : 100 s := Square{Side: 20} printArea(s) //Area of shape is : 400 r := Rectangle{Length: 20, Breadth: 10} printArea(r) //Area of shape is : 200}
本文鏈接:http://www.www897cc.com/showinfo-26-57876-0.htmlGo語言中的面向?qū)ο缶幊?OOP)
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com