Go語言程式演示類中的封裝


在本文中,我們將學習如何使用 Go 語言程式在類中實現封裝。

Go 語言中的封裝與其他面嚮物件語言的比較 - 在面嚮物件語言中,類的變數或資料對該類是私有的,只能透過宣告它們的類的成員函式訪問。然而,Go 語言不支援類和物件。因此,在 Go 語言中使用包來實現封裝。Go 提供了兩種識別符號:匯出識別符號和未匯出識別符號。從包中匯出變數、函式、方法、欄位和結構體可以實現封裝,並幫助管理元素的可見性(變數、函式、方法、欄位、結構體)。如果它們所在的包存在於你的程式中,這些元素就是可見的。

  • Go 程式語言中的匯出識別符號 - 從其所在的包匯出的識別符號稱為匯出識別符號。這些識別符號總是以大寫字母開頭。大寫字母表示一個匯出的識別符號,這就是給定識別符號的含義。匯出的識別符號始終只在其定義的包內有效。匯出包中的識別符號時,你只需匯出提供的識別符號的名稱,而不是其實現。此外,此方法也可用於欄位、方法和結構體。

  • Go 程式語言中的未匯出識別符號 - 從任何包中未匯出的識別符號稱為未匯出識別符號。它們都小寫。如下例所示,addition 函式與任何包無關,因此它不是匯出函式,其可見性僅限於此應用程式。

示例 1

使用匯出識別符號在 Go 程式語言中實現封裝 -

現在讓我們來看一個例子,我們將嘗試透過使用匯出函式的概念將字串陣列轉換為大寫。

package main

import (
   "fmt"
   "strings"
)

// fmt package allows us to print anything on the screen.
// strings package allows us to use other predefined functions like ToUpper()

// calling the main function
func main() {

   // creating an array of strings and assigning values to it
   arr := []string{"apple", "banana", "fruits"}

   // converting the letters of the string declared above to uppercase using ToUpper()
   // method defined in strings package.
   fmt.Println("Successfully converted array of strings to upper case using Exported method ToUpper() defined in strings package")
   fmt.Println("The resultant string is:")
   for x := 0; x < len(arr); x++ {

      // calling the exported method ToUpper()
      // storing the result in a new array called results
      results := strings.ToUpper(arr[x])

      // printing the result on the screen
      fmt.Println(results)
   }
}

輸出

Successfully converted array of strings to upper case using Exported method ToUpper() defined in strings package
The resultant string is:
APPLE
BANANA
FRUITS

描述

  • 首先,我們需要匯入所需的包,例如 fmt 和 strings。fmt 包允許我們在螢幕上列印任何內容,strings 包允許我們使用其中定義的其他預定義方法,例如 ToUpper()。

  • 呼叫 main 函式。這是程式的起點,程式將從此處開始。

  • 初始化一個字串陣列並將字串值儲存到其中。

  • 現在開始一個 for 迴圈來遍歷陣列,並使用 string.ToUpper() 函式將陣列的每個元素轉換為大寫,並將結果陣列儲存在 results 中。

  • 現在,使用 fmt.Println() 函式在螢幕上列印結果。

示例 2

使用未匯出識別符號在 Go 程式語言中實現封裝 -

現在讓我們來看一個例子,我們將嘗試透過使用未匯出函式的概念來查詢整數陣列的總和。

package main
import "fmt"

// fmt package allows us to print anything on the screen
// defining an unexported function addition to find the sum of an array of integers
// this function receives an array of integers as an argument and returns the integer value as the sum
func addition(val []int) int {
   s := 0

   for x := range val {
      s += val[x]
   }
   return s
}

// Calling the main function
func main() {

   // defining an array of integers and storing values in it
   arr := []int{50, 29, 36, 55, 87, 95}

   // calling then unexported method addition() to find the sum of the array and passing the
   // array to it as
   // an argument and storing the result in a separate variable
   result := addition(arr)

   // printing the result on the screen
   fmt.Println("Successfully found the sum of an array of integers using UNExported method addition()")
   fmt.Println("The resultant sum is:")
   fmt.Println(result)
}

輸出

Successfully found the sum of an array of integers using UNExported method addition()
The resultant sum is:
352

描述

  • 首先,我們需要匯入 fmt 包。fmt 包允許我們在螢幕上列印任何內容。

  • 初始化並定義一個名為 addition() 的方法來查詢整數陣列的總和。此函式將整數陣列作為引數,並計算其總和。然後它返回結果。

  • 呼叫 main 函式。這是程式的起點,程式將從此處開始。

  • 初始化一個整數陣列並將值儲存到其中。

  • 現在透過將陣列作為引數傳遞給它來呼叫 addition 函式。請注意,在呼叫 addition 函式時,第一個字母是小寫的,這表示該函式是未匯出的,並且在 main 函式中定義。

  • 現在,將結果儲存在不同的變數中並在螢幕上列印它。

更新於:2022-12-29

瀏覽量 367

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.