GO Method set方法集

下面这段文字摘自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 main
import "fmt"
type INTEGER int
func (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.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
}
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.






  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

历史五千年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值