Go 面向对象 - New() 函数 替代 构造函数

我们在 GO 面向对象 - Structs 替代 Classes 这篇文章中写的程序看起来不错,但其中有一个微妙的问题。 让我们看看当我们用零值定义 employee 结构体时会发生什么。 将 main.go 的内容替换为以下代码

main.go

package main

import"oop/employee"

funcmain() {

var e employee.Employee

e.LeavesRemaining()

}

我们所做的唯一改变是在第6行创建一个零值的 employee。该程序将输出以下内容

has 0 leaves remaining

go oop运行结果

正如你所见,使用 Employee 的零值创建的变量不可用。它没有有效的 first name、last name,也没有有效的 leave 详情。

在java等其他OOP语言中,这个问题可以通过使用构造函数来解决。可以使用参数化构造函数创建有效对象。

Go 不支持构造函数。如果类型的零值不可用,则程序员的工作是取消导出该类型以防止其他包访问,并提供一个名为 NewT(parameters) 的函数,该函数使用所需的值初始化类型 T。将创建 T 类型值的函数命名为 NewT(parameters) 是 Go 中的约定。它将充当构造函数的角色。如果包只定义了一种类型,那么 Go 中的约定就是将此函数命名为 New(parameters) 而不是 **NewT(parameters)**。

让我们对我们编写的程序进行更改,以便每次创建 Employee 时都可以使用它。

第一步是取消导出 Employee 结构体并创建一个函数 New() 来创建一个新的 Employee。将employee.go中的代码替换为以下内容

employee.go

package employee

import (

"fmt"

)

type employee struct {

firstName string

lastName string

totalLeaves int

leavesTaken int

}

funcNew(firstName string, lastName string, totalLeave int, leavesTaken int)employee {

e := employee {firstName, lastName, totalLeave, leavesTaken}

return e

}

func(e employee)LeavesRemaining() {

fmt.Printf("%s %s has %d leaves remaining\n", e.firstName, e.lastName, (e.totalLeaves - e.leavesTaken))

}

我们在这里做了一些重要的更改。我们在第7行将 Employee struct 的起始字母 E 改为小写,即我们将 Employee struct 类型改为 employee struct 类型。通过这样的修改,我们成功地取消了 employee 结构体的导出并阻止了其他包的访问。将未导出结构体的所有字段也设为未导出是一种很好的做法,除非有特定需求需要你导出它们。由于我们不需要在employee 包之外的任何地方访问employee 结构体的字段,因此我们也取消了所有字段的导出。

我们在 LeavesRemaining() 方法中相应地更改了字段名称。

现在,由于 employee 未导出,因此无法从其他包创建 employee 类型的值。因此,我们提供了一个导出用的 New 函数。 它将所需的参数作为输入并返回一个新创建的 employee。

该程序仍需进行更改以使其正常工作,但让我们运行它以了解到目前为止所做更改的效果。如果运行此程序,它会失败并显示以下编译错误

# oop

./main.go:6:8: undefined: employee.Employee

这是因为我们在 employee 包中有一个未导出的员工,并且无法从主包中访问它。 因此编译器会抛出一个错误,表明该类型未在 main.go 中定义。 完美的。 正是我们想要的。 现在没有其他软件包能够创建零值 employee 。 我们已成功阻止创建不可用的 employee 结构体值。 现在创建员工的唯一方法是使用 New 函数。

将 main.go 的内容替换为以下内容

main.go

package main

import"oop/employee"

funcmain() {

e := employee.New("Sam", "Adolf", 30, 20)

e.LeavesRemaining()

}

对该文件的唯一更改是第6行。通过将所需参数传递给新 New 函数,我们创建了一个新 employee(员工)。

下面是进行必要更改后的两个文件的内容。

employee.go

package employee

import (

"fmt"

)

type employee struct {

firstName string

lastName string

totalLeaves int

leavesTaken int

}

funcNew(firstName string, lastName string, totalLeave int, leavesTaken int)employee {

e := employee {firstName, lastName, totalLeave, leavesTaken}

return e

}

func(e employee)LeavesRemaining() {

fmt.Printf("%s %s has %d leaves remaining\n", e.firstName, e.lastName, (e.totalLeaves - e.leavesTaken))

}

main.go

package main

import"oop/employee"

funcmain() {

e := employee.New("Sam", "Adolf", 30, 20)

e.LeavesRemaining()

}

执行以下命令

$ go install oop

$ oop

Sam Adolf has 10 leaves remaining

go oop New函数执行结果

因此你可以理解,虽然 Go 不支持类,但可以有效地使用结构体代替类,并且可以使用全局函数 New(parameters) 来代替构造函数。

和GO 面向对象 - Structs 替代 Classes这篇文章一起,构成了 Go 中的类和构造函数。

本文转载自:迹忆客(https://www.jiyik.com)

以上是 Go 面向对象 - New() 函数 替代 构造函数 的全部内容, 来源链接: utcz.com/z/290228.html

回到顶部