
尽管GO语言没有封装,继承,多态这些概念,但同样通过别的方式实现这些特性
封装:通过方法实现
继承:通过匿名字段实现
多态:通过接口实现
匿名字段



方法method
function (receiver ReceiverType) funcName ()
带有 接收者的 函数 就是方法, 只要接受者reciver类型不一样,同函数名都属于不同的函数
Go语言中可以给任意 自定义类型 添加相应的方法
接收者类型不能为指针类型


// 注意:reciverType接收着类型本身不能是指针类型,否则报错
ep: type pointer *int 如果reciverType是pointer报错
// invalid receiver type pointer (pointer is a pointer type)
type pointer *int
func (p pointer) test() {}
方法集
类型的方法集 指 可以被该类型的值调用的所有方法的集合
用 实例value 和指针pointer调用方法不受方法集约束,编译器能找到所有的方法,并自动转换receiver实参



网友评论