使用方法而不是函数有什么好处? [英] Is there any profits of using Methods rather than Functions?

查看:48
本文介绍了使用方法而不是函数有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在出于练习Go的目的而开发"Matrix"结构和相关方法.我做了很多方法,但我意识到所有这些方法都可以更改为函数我已经习惯了C ++,在C ++中,如果我制作了一个参数为类类型的函数,则该函数将无法使用该类的私有变量(信息隐藏)但是,当我使用"Go"构建类似的代码时,函数可以访问结构的变量.所以我没有得到Go中方法和函数之间的区别.使用方法而不是功能会产生任何利润,反之亦然吗?

I'm developing "Matrix" struct and related methods for the purpose of practicing Go. I made a lot of methods but I realized that all these methods can be changed into functions I'm used to C++ and in C++, if I make a function whose parameter is a type of class, the function cannot use the class' private variable(information hiding) However, when I built a similar code using "Go", a function can access a struct's variable. So I don't get what is different between methods and functions in Go. Are there any profits using methods rather than functions or vice versa?

第一个是我原始的矩阵"代码(不是全部)它使用了一种方法"Tr".它没有问题.

First one is my original "Matrix" code(not all of it) It used a method "Tr". It doesn't have problems.

package main
import "fmt"

//definition of "Array"
type Array struct{
    component [][]float32
    row int
    col int
}
//constructor of Array, "Ones"; making an array setting all component as one
func Ones(m int, n int) Array{
    var a Array
    a.component = make([][]float32, m)
    a.row=m
    a.col=n
    for i:=0; i<m; i++{
        a.component[i] = make([]float32, n)
        for j:=0; j<n; j++{
            a.component[i][j]=1
        }
    }
    return a
}
//Tr function; find trace of an Array
func (a Array) Tr() float32{
    var sum float32 = 0
    for i:=0; i<a.row; i++{
        sum += a.component[i][i]
    }
    return sum
}

func main(){
    a := Ones(3,3)
    fmt.Println(a.Tr())
}

第二个是另一个类似的代码.(除"Tr"部分外其他所有内容均相同)它仅使用功能.它也没有问题.

The second one is another similar code. (Everything is same but "Tr" part) It used only functions. It also doesn't have problems.

package main
import "fmt"

//definition of "Array"
type Array struct{
    component [][]float32
    row int
    col int
}
//constructor of Array, "Ones"; making an array setting all component as one
func Ones(m int, n int) Array{
    var a Array
    a.component = make([][]float32, m)
    a.row=m
    a.col=n
    for i:=0; i<m; i++{
        a.component[i] = make([]float32, n)
        for j:=0; j<n; j++{
            a.component[i][j]=1
        }
    }
    return a
}
//Tr function; find trace of an Array
func Tr(a Array) float32{
    var sum float32 = 0
    for i:=0; i<a.row; i++{
        sum += a.component[i][i]
    }
    return sum
}

func main(){
    a := Ones(3,3)
    fmt.Println(Tr(a))
}

推荐答案

如果只想调用函数或方法,没关系,您可以创建一个带有签名的函数,其中接收者是普通的,常规的范围.不会有任何性能上的损失(方法可能是 虚拟,但在Go中没有虚拟方法.

If you just want to call the function or method, it doesn't matter, you may create a function with a signature where the receiver is a normal, regular parameter. There won't be any performance penalty (there could be if methods could be virtual, but in Go there are no virtual methods).

优势之一可能是视觉吸引力".调用方法可以使其明显属于接收方.如果使用方法,我也会发现链接的代码更容易理解.

One advantage might be the "visual appeal". Calling a method makes it obvious it belongs to the receiver. I also find chained code easier to understand if methods are used.

比较不带方法的此解决方案:

Compare this solution without methods:

type Circle struct{}
type Point struct{}

func Center(Circle) Point { return Point{} }
func Abs(Point) float64   { return 0 }

func main() {
    var c Circle
    fmt.Println(Abs(Center(c)))
}

Abs(Center(c))不是那么直观.但是,如果您添加方法而不是使用函数:

Abs(Center(c)) isn't that intuitive. But if you add methods instead of using functions:

func (Circle) Center() Point { return Point{} }
func (Point) Abs() float64 { return 0 }

func main() {
    var c Circle
    fmt.Println(c.Center().Abs())
}

c.Center().Abs()更容易理解.

如果要实现接口,则必须使用方法.如果接口包含某些方法,则只有具有那些方法的类型才能实现它.参见相关内容:为什么在Golang中需要接口?还应注意,您只能创建在同一程序包中定义的方法,因此,如果您要武装"来自其他程序包的类型,则不能使用"方法.

Methods are a must if you want to implement interfaces. If an interface contains some methods, only types that have those methods can implement it. See related: Why are interfaces needed in Golang? It should also be noted that you may only create methods defined in the same package, so if you want to "arm" a type from a different package, you can't "use" methods.

我将使用方法称为利润"的一件事:您不能按名称调用函数,但是可以按名称访问和调用方法.有关详细信息,请参见使用特殊前缀/后缀的调用函数.

One thing that I would call "profit" for using methods: you can't call functions by name, but you can access and call methods by name. For details, see Call functions with special prefix/suffix.

这篇关于使用方法而不是函数有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆