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 函数中定义。
现在,将结果存储在不同的变量中并在屏幕上打印它。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP