Go 语言中的封装机制深入理解一下

2023-06-1017:43:52编程语言入门到精通Comments1,022 views字数 2968阅读模式

什么是封装

封装,也就是所谓的信息隐藏,是面向对象编程的一个重要方面。封装被定义为将数据包裹在一个单一的单元下。它是将代码和它所操作的数据绑定在一起的机制。从另一个角度来说,封装是一个保护罩,防止数据被这个保护罩外的代码访问。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46831.html

在面向对象的语言中,一个类的变量或数据是隐藏的,不受任何其他类的影响,只能通过它们所声明的自己类的任何成员函数来访问。但是 Go 语言不支持类和对象。因此,在 Go 语言中,封装是通过使用包来实现的。Go 语言提供两种不同类型的标识符,即可导出 exported 的和不被导出 unexported 的标识符。封装是通过从包中导出的元素(变量、函数、方法、字段、结构)实现的,它有助于控制元素(变量、函数、方法、字段、结构)的可见性。如果定义这些元素的包在你的程序中可用,那么这些元素就是可见的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46831.html

Go 中的封装

Go 语言中的封装机制深入理解一下

Go 与经典的 OOP 语言不同。它没有类、对象和访问修改器的概念。相反,它有一个包的概念。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46831.html

Golang 在包的层次上提供封装。Go 没有任何 publicprivate 或 protected 关键字。控制可见性的唯一机制是使用大写和非大写的格式:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46831.html

  • 大写的标识符是可以导出的。大写字母表示这是一个导出的标识符。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46831.html
  • 非大写的标识符不被导出。小写表示该标识符不被导出,只在同一包内被访问。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46831.html

有五种可以导出或不能被导出的标识符:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46831.html

  1. 结构体文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46831.html
  2. 结构体方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46831.html
  3. 结构体字段文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46831.html
  4. 函数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46831.html
  5. 变量文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46831.html

让我们看一个显示上述所有标识符的导出和不导出的示例。请参阅下面的 data.go:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46831.html

package model

import "fmt"

var (
 SchoolName     = "test"
 schoolLocation = "somecity"
)

//Person struct
type Person struct {
 Name string
 age  int
}

//GetAge of person
func (p *Person) GetAge() int {
 return p.age
}

func (p *Person) getName() string {

 return p.Name

}

type school struct {
}

//GetPerson get the person object

func GetPerson() *Person {
 p := &Person{
  Name: "test",
  age:  21,
 }

 fmt.Println("Model Package:")

 fmt.Println(p.Name)

 fmt.Println(p.age)

 return p
}

func getSchoolName() string {

 return SchoolName

}

测试 test.go

package model

import "fmt"

//Test function
func Test() {

 p := &Person{
  Name: "Jimmy",
  age:  26,
 }
 fmt.Println(p)
 c := &school{}
 fmt.Println(c)

 //STRUCTURE'S METHOD
 fmt.Println(p.GetAge())
 fmt.Println(p.getName())

 //STRUCTURE'S FIELDS
 fmt.Println(p.Name)
 fmt.Println(p.age)

 //FUNCTION
 person2 := GetPerson()
 fmt.Println(person2)
 schoolName := getSchoolName()
 fmt.Println(schoolName)

 //VARIBLES
 fmt.Println(schoolLocation)
 fmt.Println(SchoolName)
}

在运行这个文件时,它能够访问 data.go 中所有导出的和未导出的字段,因为两者都在同一个包模型中。没有编译错误,它的输出如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46831.html

&{test 26}
&{}
26
test
test
26
Model Package:
test
26
&{test 26}
test
somecity
test

让我们把上述文件 test.go 移到另一个名为 view 的包中。现在注意运行 go build 的输出。它给出了编译错误。所有的编译错误都是由于不能引用未导出的字段造成的:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46831.html

package view

import "fmt"

//Test function
func Test() {
    //STRUCTURE IDENTIFIER
    p := &model.Person{
        Name: "test",
        age:  21,
    }
    fmt.Println(p)
    c := &model.company{}
    fmt.Println(c)
    
    //STRUCTURE'S METHOD
    fmt.Println(p.GetAge())
    fmt.Println(p.getName())
    
    //STRUCTURE'S FIELDS
    fmt.Println(p.Name)
    fmt.Println(p.age)
    
    //FUNCTION
    person2 := model.GetPerson()
    fmt.Println(person2)
    schoolName := model.getSchoolName()
    fmt.Println(companyName)
    
    //VARIBLES
    fmt.Println(model.schoolLocation)
    fmt.Println(model.SchoolName)
}

输出:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46831.html

test.go:13:3: unknown field 'age' in struct literal of type model.Person
test.go:17:8: cannot refer to unexported name model.school
test.go:17:8: undefined: model.school
test.go:22:15: p.getName undefined (cannot refer to unexported field or method model.(*Person).getName)
test.go:26:15: p.age undefined (cannot refer to unexported field or method age)
test.go:31:17: cannot refer to unexported name model.getSchoolName
test.go:31:17: undefined: model.getSchoolName
test.go:35:14: cannot refer to unexported name model.schoolLocation
test.go:35:14: undefined: model.schoolLocation
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/46831.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/46831.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定