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

首頁 后端開發(fā) Golang Golang 中的面向?qū)ο缶幊?(OOP) 簡介

Golang 中的面向?qū)ο缶幊?(OOP) 簡介

Dec 23, 2024 pm 12:43 PM

當我們談論編程時,我們通常指的是編寫一堆修改某些數(shù)據(jù)并與某些數(shù)據(jù)交互的函數(shù)。面向?qū)ο缶幊蹋∣OP)是一種編程模型,它專注于包含數(shù)據(jù)并附加一些相關功能的“對象”。面向?qū)ο缶幊逃兴拇笾е豪^承、封裝、多態(tài)性和抽象。在本博客中,我們將通過示例了解如何在 Golang 中實現(xiàn)它們中的每一個。推薦一些有關 OOP 的基本概念,但如果沒有,我將簡要介紹所有四個支柱的含義。

Introduction to Object Oriented Programming (OOP) in Golang

類、對象和方法

面向?qū)ο缶幊痰暮诵乃枷肟梢愿爬橐韵聨c:

  • 您定義“類”,它們是數(shù)據(jù)和可以調(diào)用該數(shù)據(jù)的函數(shù)的集合。
  • 這些特定的函數(shù)稱為該特定類的“方法”。
  • 類的實際實例稱為“對象”。

讓我們看一些 Golang 中的代碼來理解這三個概念:

package main

import "fmt"

type Batman struct {
    actor string
    year int
}

func (b Batman) SayImBatman() {
    fmt.Printf("I'm %s and I'm Batman from year %d\n", b.actor, b.year)
}

func main() {
    b1 := Batman{actor: "Michael Keaton", year: 1989}
    b2 := Batman{actor: "Christian Bale", year: 2005}

    b1.SayImBatman()
    b2.SayImBatman()
}

在Golang中,類只不過是我們定義的類型。這些類型不一定必須是結構體,但通常都是結構體,因為在 OOP 中我們使用數(shù)據(jù)集合,這些數(shù)據(jù)可以是任何類型(字符串、整數(shù)等)。

類是對象的藍圖。每當實例化一個類時,就會形成一個對象。在此示例中,b1 和 b2 是 Batman 類的對象。

SayImBatman 函數(shù)可以在該類的任何對象上調(diào)用。由于它與 Batman 類相關,因此不將其稱為常規(guī)函數(shù),而是將其稱為類的方法。

我認為這應該足以讓您清楚 OOP 的基礎知識,以便您繼續(xù)進行下一部分,我們將在其中介紹 OOP 的四大支柱。

遺產(chǎn)

繼承引入了 OOP 中 parentchild 類的概念。子類是從父類派生的類,并繼承其所有方法和屬性(數(shù)據(jù))。讓我們看一些代碼來幫助我們理解這一點:

package main

import "fmt"

type Hero struct {
    team string
}

type Batman struct {
    Hero
    name string
}

type Ironman struct {
    Hero
    power int
}

func (h Hero) SayTeam() {
    fmt.Println("My Team is", h.team)
}

func (b Batman) SayImBatman() {
    fmt.Printf("I'm %s and I'm Batman\n", b.name)
}

func (i Ironman) SayPowerLevel() {
    fmt.Printf("I'm Ironman and my powerlevel is %d\n", i.power)
}

func main() {
    b1 := Batman{Hero{team: "Justice League"}, "Christian Bale"}
    i1 := Ironman{Hero{team: "Avengers"}, 23}

    b1.SayImBatman()
    b1.SayTeam()

    i1.SayPowerLevel()
    i1.SayTeam()
}

在此示例中,蝙蝠俠和鋼鐵俠是 Hero 父類的子類。他們可以訪問其父類的屬性(即 team)及其方法(即 SayTeam)。正如您在聲明 b1 和 i1 實例時所看到的那樣,我們指定了父類屬性以及它們各自類的特定屬性。它們都能夠調(diào)用父類中定義的 SayTeam 方法。但它們也有各自獨特的屬性和方法。

Golang 使用組合(在結構體中使用結構體)來實現(xiàn)繼承。它不像其他 OOP 語言(例如 C 或 Java)那樣具有內(nèi)置的基于類的繼承。

封裝

封裝是隱藏對象內(nèi)部屬性并且不允許直接修改它們的原理。相反,它依賴于提供獲取和更新這些屬性的方法。讓我們看一個例子來更好地理解這一點:

package main

import "fmt"

type Batman struct {
    actor string
    year int
}

func (b Batman) SayImBatman() {
    fmt.Printf("I'm %s and I'm Batman from year %d\n", b.actor, b.year)
}

func main() {
    b1 := Batman{actor: "Michael Keaton", year: 1989}
    b2 := Batman{actor: "Christian Bale", year: 2005}

    b1.SayImBatman()
    b2.SayImBatman()
}

package main

import "fmt"

type Hero struct {
    team string
}

type Batman struct {
    Hero
    name string
}

type Ironman struct {
    Hero
    power int
}

func (h Hero) SayTeam() {
    fmt.Println("My Team is", h.team)
}

func (b Batman) SayImBatman() {
    fmt.Printf("I'm %s and I'm Batman\n", b.name)
}

func (i Ironman) SayPowerLevel() {
    fmt.Printf("I'm Ironman and my powerlevel is %d\n", i.power)
}

func main() {
    b1 := Batman{Hero{team: "Justice League"}, "Christian Bale"}
    i1 := Ironman{Hero{team: "Avengers"}, 23}

    b1.SayImBatman()
    b1.SayTeam()

    i1.SayPowerLevel()
    i1.SayTeam()
}

在Golang中,從包中導出的屬性和方法以大寫字母開頭。當我們在utils包中定義小寫的actor和year時,我們確保它們不能被直接修改。相反,就像您在 main.go 文件中看到的那樣,您需要使用導出的方法(以大寫字母開頭) - GetActor、SetActor 等來獲取和修改它們。

這就是封裝的全部內(nèi)容 - 確保防止數(shù)據(jù)意外更改,并提供與數(shù)據(jù)安全交互的方法。

您會注意到不同的一件事是,在 Batman 類的所有方法中,我們使用指針接收器 *Batman 而不是像前面示例中那樣使用值接收器 Batman。這是因為我們希望能夠在 Set 方法中修改原始結構。在 Golang 中,最佳實踐是,如果某些方法需要指針接收器,則讓所有方法都使用指針接收器以保持一致性。這就是為什么 Get 方法也使用指針接收器,即使它們沒有修改原始結構。

此外,還要注意的一件事是,僅僅因為我們使用的是指針接收器,我們就不必這樣做:(&b1).GetActor。在 Golang 中,帶有指針參數(shù)的函數(shù)必須采用指針,但帶有指針接收器的方法可以采用值或指針作為接收器。

TL;DR:Golang 自動將 b1.GetActor 轉(zhuǎn)換為 (&b1).GetActor,因為 GetActor 方法有一個指針接收器,但如果 GetActor 是一個普通函數(shù),它不會將 GetActor(b1) 轉(zhuǎn)換為 GetActor(&b1)一個指針參數(shù)。

多態(tài)性和抽象

OOP 的接下來兩個支柱可以合并在一起,因為它們的代碼示例看起來非常相似。多態(tài)性是指兩個不同類的兩個不同對象可以被視為同一公共超類的對象的編程實踐。這意味著您可以在兩個不同的對象上調(diào)用相同的函數(shù),就好像它們是同一類的對象一樣。這應該開始讓您感受到所涉及的接口的味道:)

讓我們看一些代碼來更好地理解這一點:

package main

import "fmt"

type Batman struct {
    actor string
    year int
}

func (b Batman) SayImBatman() {
    fmt.Printf("I'm %s and I'm Batman from year %d\n", b.actor, b.year)
}

func main() {
    b1 := Batman{actor: "Michael Keaton", year: 1989}
    b2 := Batman{actor: "Christian Bale", year: 2005}

    b1.SayImBatman()
    b2.SayImBatman()
}

在此示例中,StartFight 函數(shù)可以傳遞 b1 和 i1 對象,即使它們彼此沒有任何關系。嘗試理解這與繼承有何不同,在繼承中,子類可以訪問父類的方法。在此示例中,沒有子類和父類(也沒有共享方法)。相反,兩個不同的對象被函數(shù)視為相同:這稱為多態(tài)性。

現(xiàn)在,這也可以被視為抽象的一個例子。顧名思義,抽象是一種隱藏實現(xiàn)細節(jié),而只是提供為您處理事務的函數(shù)的編程實踐。在這個例子中,你不需要關心單個英雄的方法是如何配置的。當您想使用任何英雄的戰(zhàn)斗功能時,您可以隨時繼續(xù)使用開始戰(zhàn)斗功能。這樣,實現(xiàn)細節(jié)對用戶來說是隱藏的,只暴露基本的細節(jié)。

現(xiàn)在回到多態(tài),有兩個更常見的例子,那就是方法重寫和重載。

方法重寫

方法重寫是指子類定義自己的父類中定義的方法的實現(xiàn)?,F(xiàn)在使用此實現(xiàn)來代替原始父類的實現(xiàn)。讓我們看看之前用于繼承的代碼,看看它在方法重寫后的樣子:

package main

import "fmt"

type Hero struct {
    team string
}

type Batman struct {
    Hero
    name string
}

type Ironman struct {
    Hero
    power int
}

func (h Hero) SayTeam() {
    fmt.Println("My Team is", h.team)
}

func (b Batman) SayImBatman() {
    fmt.Printf("I'm %s and I'm Batman\n", b.name)
}

func (i Ironman) SayPowerLevel() {
    fmt.Printf("I'm Ironman and my powerlevel is %d\n", i.power)
}

func main() {
    b1 := Batman{Hero{team: "Justice League"}, "Christian Bale"}
    i1 := Ironman{Hero{team: "Avengers"}, 23}

    b1.SayImBatman()
    b1.SayTeam()

    i1.SayPowerLevel()
    i1.SayTeam()
}

該程序的輸出是:

//oops-in-go/utils/utils.go

package utils

type Batman struct {
    actor string
    year int
}

func (b *Batman) GetActor() string {
    return b.actor
}

func (b *Batman) GetYear() int {
    return b.year
}

func (b *Batman) SetActor(actor string) {
    b.actor = actor
}

func (b *Batman) SetYear(year int) {
    b.year = year
}

Batman 類的對象現(xiàn)在使用自己的 SayTeam 方法,而不是父 Hero 類的方法。由于 Ironman 類沒有自己的 SayTeam 方法,因此它的對象仍然使用其父類的方法。這就是方法重寫的意思,子類“重寫”父類中定義的方法。

方法重載

這是指同一個函數(shù)能夠接受多個不同的參數(shù)。這些參數(shù)的數(shù)量或類型可能不同。 Golang 提供了兩種方法來實現(xiàn)這一點:通過可變參數(shù)函數(shù),另一種通過接口。

讓我們看一下兩者的代碼,這將幫助您更好地理解:

使用可變參數(shù)函數(shù)

// oops-in-go/main.go

package main

import (
    "fmt"
    "oops-in-go/utils"
)

func main() {
    b1 := utils.Batman{}
    b1.SetActor("Michael Keaton")
    b1.SetYear(1989)
    fmt.Printf("I'm %s and I'm Batman from year %d\n", b1.GetActor(), b1.GetYear())

    b1.SetActor("Christian Bale")
    b1.SetYear(2005)
    fmt.Printf("I'm %s and I'm Batman from year %d\n", b1.GetActor(), b1.GetYear())
}

在這里,您可以使用任意數(shù)量個參數(shù)“重載”listMembers函數(shù)。

使用接口

package main

import "fmt"

type Hero interface {
    Fight()
}

type Batman struct {
    weapon string
}

type Ironman struct {
    weapon string
}

func (b Batman) Fight() {
    fmt.Printf("Batman hits with a %s\n", b.weapon)
}

func (i Ironman) Fight() {
    fmt.Printf("Ironman hits with a %s\n", i.weapon)
}

func StartFight(h Hero) {
    fmt.Println("Fight has started.")
    h.Fight()
}

func main() {
    b1 := Batman{"Batarang"}
    i1 := Ironman{"Repulsor rays"}

    StartFight(b1)
    StartFight(i1)
}

該程序的輸出是:

package main

import "fmt"

type Hero struct {
    team string
}

type Batman struct {
    Hero
    name string
}

type Ironman struct {
    Hero
    power int
}

func (h Hero) SayTeam() {
    fmt.Println("My Team is", h.team)
}

func (b Batman) SayImBatman() {
    fmt.Printf("I'm %s and I'm Batman\n", b.name)
}

func (i Ironman) SayPowerLevel() {
    fmt.Printf("I'm Ironman and my powerlevel is %d\n", i.power)
}

func (b Batman) SayTeam() {
    fmt.Printf("I'm Batman and my team is %s\n", b.team)
}

func main() {
    b1 := Batman{Hero{team: "Justice League"}, "Christian Bale"}
    i1 := Ironman{Hero{team: "Avengers"}, 23}

    b1.SayImBatman()
    b1.SayTeam()

    i1.SayPowerLevel()
    i1.SayTeam()
}

這里我們“重載”了 saySomething 方法來接受不同類型的參數(shù)。我們采用一個空接口作為參數(shù),它可以是任何類型,然后使用 switch case 檢查其類型并相應地打印輸出。

結論

我很清楚這是一篇很長的文章,如果您堅持讀到最后,我想讓您知道我真的很高興:) 我真誠地希望您學到很多有關面向?qū)ο缶幊痰男轮R以及如何在 Golang 中實現(xiàn)它。我在我的網(wǎng)站上撰寫有關不同技術概念的博客,如果您有興趣學習新事物,我建議您訂閱我的時事通訊。

以上是Golang 中的面向?qū)ο缶幊?(OOP) 簡介的詳細內(nèi)容。更多信息請關注PHP中文網(wǎng)其他相關文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻,版權歸原作者所有,本站不承擔相應法律責任。如您發(fā)現(xiàn)有涉嫌抄襲侵權的內(nèi)容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動的應用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機

Video Face Swap

Video Face Swap

使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

將Golang服務與現(xiàn)有Python基礎架構集成的策略 將Golang服務與現(xiàn)有Python基礎架構集成的策略 Jul 02, 2025 pm 04:39 PM

TOIntegrategolangServicesWithExistingPypythoninFrasture,userestapisorgrpcForinter-serviceCommunication,允許GoandGoandPyThonAppStoStoInteractSeamlessSeamLlyThroughlyThroughStandArdArdAdrotized Protoccols.1.usererestapis(ViaFrameWorkslikeSlikeSlikeGiningOandFlaskInpyThon)Orgrococo(wirs Propococo)

了解Web API的Golang和Python之間的性能差異 了解Web API的Golang和Python之間的性能差異 Jul 03, 2025 am 02:40 AM

Golangofferssuperiorperformance,nativeconcurrencyviagoroutines,andefficientresourceusage,makingitidealforhigh-traffic,low-latencyAPIs;2.Python,whileslowerduetointerpretationandtheGIL,provideseasierdevelopment,arichecosystem,andisbettersuitedforI/O-bo

是Golang前端還是后端 是Golang前端還是后端 Jul 08, 2025 am 01:44 AM

Golang主要用于后端開發(fā),但也能在前端領域間接發(fā)揮作用。其設計目標聚焦高性能、并發(fā)處理和系統(tǒng)級編程,適合構建API服務器、微服務、分布式系統(tǒng)、數(shù)據(jù)庫操作及CLI工具等后端應用。雖然Golang不是網(wǎng)頁前端的主流語言,但可通過GopherJS編譯成JavaScript、通過TinyGo運行于WebAssembly,或搭配模板引擎生成HTML頁面來參與前端開發(fā)。然而,現(xiàn)代前端開發(fā)仍需依賴JavaScript/TypeScript及其生態(tài)。因此,Golang更適合以高性能后端為核心的技術棧選擇。

如何完全,干凈地從我的系統(tǒng)中卸載Golang? 如何完全,干凈地從我的系統(tǒng)中卸載Golang? Jun 30, 2025 am 01:58 AM

TocompletelyuninstallGolang,firstdeterminehowitwasinstalled(packagemanager,binary,source,etc.),thenremoveGobinariesanddirectories,cleanupenvironmentvariables,anddeleterelatedtoolsandcaches.Beginbycheckinginstallationmethod:commonmethodsincludepackage

如何使用頻道在Golang的Goroutines之間進行通信? 如何使用頻道在Golang的Goroutines之間進行通信? Jun 26, 2025 pm 12:08 PM

Go語言中channel用于goroutine間通信與同步。聲明使用make函數(shù),如ch:=make(chanstring),發(fā)送用ch

在構建過程中,'找不到軟件包”錯誤是什么意思? 在構建過程中,'找不到軟件包”錯誤是什么意思? Jun 26, 2025 pm 12:57 PM

當遇到“cannotfindpackage”錯誤時,通常是因為Go無法找到目標包或依賴。解決方法如下:1.檢查導入路徑是否正確,確保與模塊路徑或目錄結構一致;2.確認已初始化go.mod文件,使用gomodinit和gomodtidy管理依賴;3.運行goget下載缺失依賴或清理模塊緩存;4.確保在正確的目錄上下文中執(zhí)行命令,或指定完整的模塊相對路徑進行構建。

如何在Golang中使用Select語句進行非阻滯渠道操作和超時? 如何在Golang中使用Select語句進行非阻滯渠道操作和超時? Jun 26, 2025 pm 01:08 PM

在Go中,使用select語句可以有效處理非阻塞通道操作和實現(xiàn)超時機制。通過default分支實現(xiàn)非阻塞接收或發(fā)送操作,如1.非阻塞接收:若有值則接收并打印,否則立即執(zhí)行default分支;2.非阻塞發(fā)送:若通道無接收者則跳過發(fā)送。此外,結合time.After可實現(xiàn)超時控制,例如等待結果或2秒后超時返回。還可組合非阻塞與超時行為,先嘗試立即獲取值,失敗后再短暫等待,提升程序并發(fā)響應能力。

如何使用自定義字段名稱將golang結構元載到JSON? 如何使用自定義字段名稱將golang結構元載到JSON? Jun 30, 2025 am 01:59 AM

在Go中,若希望結構體字段在轉(zhuǎn)換為JSON時使用自定義字段名,可通過結構體字段的json標簽實現(xiàn)。1.使用json:"custom_name"標簽指定字段在JSON中的鍵名,如Namestringjson:"username""會使Name字段輸出為"username";2.添加,omitempty可控制字段為空值時省略輸出,例如Emailstringjson:"email,omitempty""

See all articles