GO Method set方法集

    xiaoxiao2021-03-25  27

    下面这段文字摘自go官方文档的Language Specification。

    Method sets A type may have a method set associated with it.  The method set of an interface type is its interface.  The method set of any other type T consists of all methods declared with receiver type T.  The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T (that is, it also contains the method set of T).  Further rules apply to structs containing anonymous fields, as described in the section on struct types.  Any other type has an empty method set.  In a method set, each method must have a unique non-blank method name. The method set of a type determines the interfaces that the type implements and the methods that can be called using a receiver of that type.

    从上面这段文字可以看出:

    1、自定义类型T的方法集的特征是接收器的类型是T

    2、指针类型*T的方法集的特征是接收器的类型是*T或T

    下面进行举例说明:

    example1

    //main.go

    package main import "fmt" type INTEGER int func (a INTEGER) Less(b INTEGER) bool {     return a < b } func (a *INTEGER) Add(b INTEGER) {     *a += b } type LessAdder interface {     Less(INTEGER) bool     Add(INTEGER) } func main() {     var a INTEGER = 100     var b LessAdder = a     b.Add(30)     fmt.Println(a) }

    从上面的代码可以看出,INTEGER类型只具有Less方法,而*INTEGER类型具有Less和Add方法。

    编译运行:

    C:/go/bin/go.exe run test.go [E:/project/go/proj/src/test]

    # command-line-arguments

    .\test.go:22:cannot use a (type INTEGER) as type LessAdder in assignment:

    INTEGER does not implement LessAdder (Add method has pointer receiver)

    错误: 进程退出代码 2.

    提示信息说,INTEGER does not implement LessAdder (Add method has pointer receiver) 下面进行代码改写,去掉Add方法的指针接收器。

    example2:

    //main.go package main import "fmt" type INTEGER int func (a INTEGER) Less(b INTEGER) bool {     return a < b } func (a INTEGER) Add(b INTEGER) {     a += b } type LessAdder interface {     Less(INTEGER) bool     Add(INTEGER) } func main() {     var a INTEGER = 100     var b LessAdder = a //类型是INTEGER     b.Add(30)     fmt.Println(a)     b = &a //类型是*INTEGER     b.Add(30)     fmt.Println(a) } 从上面的代码可以看出,INTEGER和*INTEGER都实现了Less和Add方法。

    编译运行:

    C:/go/bin/go.exe run test.go [E:/project/go/proj/src/test]

    100

    100

    成功: 进程退出代码 0.

    example3:

    //main.go package main import "fmt" type INTEGER int func (a INTEGER) Less(b INTEGER) bool {     return a < b } func (a *INTEGER) Add(b INTEGER) {     *a += b } type LessAdder interface {     Less(INTEGER) bool     Add(INTEGER) } func main() {     var a INTEGER = 100     var b LessAdder = &a     b.Add(30)     fmt.Println(a) }

    这个例子中,只有*INTEGER类型同时实现了LessAdder接口,因此只有*INTEGER类型的实例才能赋值给LessAdder变量。

    编译运行:

    C:/go/bin/go.exe run test.go [E:/project/go/proj/src/test]

    130

    成功: 进程退出代码 0.

    example4:

    //main.go

    package mainimport "fmt"type INTEGER intfunc (a INTEGER) Less(b INTEGER) bool {    return a < b}func (a *INTEGER) Add(b INTEGER) {    *a += b}func main() {    var a INTEGER = 100    fmt.Println(a.Less(200))    a.Add(30) //变量a为什么可以直接调用Add方法呢,按道理,Add方法的接收器是*INTEGER,而 //a的类型是INTEGER,是因为go编译器帮我们做了转换工作。 //在这里, a.Add(30) <==> (&a).Add(30) ,引用传递    fmt.Println(a)}编译运行:

    C:/go/bin/go.exe run test4.go [E:/project/go/proj/src/test]

    true

    130

    成功: 进程退出代码 0.

    example5:

    //main.gopackage mainimport "fmt"type INTEGER intfunc (a INTEGER) Less(b INTEGER) bool {    return a < b}func (a *INTEGER) Add(b INTEGER) {    *a += b}func main() {    var a INTEGER = 100    var p = &a    p.Add(50)    fmt.Println(*p)    fmt.Println(p.Less(50)) //这里编译器也帮我们做了转换工作 // p.Less(50) <==> (*p).Less(50),值传递}编译运行:

    C:/go/bin/go.exe run test4.go [E:/project/go/proj/src/test]

    150

    false

    成功: 进程退出代码 0.

    转载请注明原文地址: https://ju.6miu.com/read-50262.html

    最新回复(0)