1 go方法与类成员函数
方法其实就是一个函数,在func这个关键字和方法名中间加入了一个特殊的接收器类型。接收器可以是结构体类型或者是非结构体类型。接收器是可以在方法的内部访问的。格式定义如下:
func (t Type) methodName(parameter list) {
}
因为Go中没有类class的概念,所以要给自定义的类型添加成员函数就无法像C++一样写在类定义中,在Go中只能使用方法这一特性变相的实现成员函数和多态:
//定义类型
type Employee struct {
name string
age int
}
//添加值接收器的方法
func (e Employee) changeName(newName string) {
e.name = newName
}
//添加指针接收器的方法
func (e *Employee) changeAge(newAge int) {
e.age = newAge
}
func main() {
e := Employee{
name: "Mark Andrew",
age: 50,
}
fmt.Printf("Employee name before change: %s", e.name)//调用属性
e.changeName("Michael Andrew")//调用值接收器,无法改变属性值
fmt.Printf("\nEmployee name after change: %s", e.name)
fmt.Printf("\n\nEmployee age before change: %d", e.age)
(&e).changeAge(51)//调用指针接收器
fmt.Printf("\nEmployee age after change: %d", e.age)
}
注意:为了在一个类型上定义一个方法,方法的接收器类型定义和方法的定义应该在同一个包中,其实就是类定义和成员函数定义要放在一起,很好理解。
2 接口
在面向对象的领域里,接口一般这样定义:接口定义一个对象的行为。接口只指定了对象应该做什么(抽象定义),至于如何实现这个行为,则由对象本身去确定(实现细节)。
2.1 接口的定义
在Go语言中,接口就是方法签名(Method Signature)的集合。当一个类型实现了接口中的所有方法,我们称它实现了该接口,这与面向对象编程(OOP)的说法很类似。我们可以用接口来实现C++面向对象的多态。
//interface definition
type interfaceName interface {
methodName(parameter list)
//...
}
使用示例如下:
//定义interface
type Describer interface {
Describe()
}
//定义类型,可以当作C++类
type Person struct {
name string
age int
}
type Address struct {
state string
country string
}
//使用值接收器实现接口
func (p Person) Describe() {
fmt.Printf("%s is %d years old\n", p.name, p.age)
}
//使用指针接收器实现接口
func (a *Address) Describe() { // 使用指针接受者实现
fmt.Printf("State %s Country %s", a.state, a.country)
}
func main() {
var d1 Describer//定义接口类型变量,类似C++基类
p1 := Person{"Sam", 25}
d1 = p1 //赋值为Person类型的对象,因为该对象已经实现接口,类似C++父类指针赋值子类对象
d1.Describe()//调用成员函数
var d2 Describer
a := Address{"Washington", "USA"}
/* 如果下面一行取消注释会导致编译错误:
cannot use a (type Address) as type Describer
in assignment: Address does not implement
Describer (Describe method has pointer
receiver)
*/
//d2 = a
d2 = &a // 这是合法的,因为Address 类型的指针实现了 Describer 接口
d2.Describe()
}
我们可以把接口看作内部的一个元组 (type, value)
- type是接口底层的具体类型(Concrete Type),可以通过
v.(type)
获取类型 - value 是具体类型的值,可以通过
v.(T)
获取,T为具体的类型,比如int2.2 接口的嵌套
Go 语言没有提供继承机制,但可以通过嵌套其他的接口,创建一个新接口:type SalaryCalculator interface { DisplaySalary() } type LeaveCalculator interface { CalculateLeavesLeft() int } //嵌套两个interface,类似C++继承两个基类 type EmployeeOperations interface { SalaryCalculator LeaveCalculator //可以再加新的方法接收器 } type Employee struct { firstName string lastName string basicPay int pf int totalLeaves int leavesTaken int } //实现接口方法 func (e Employee) DisplaySalary() { fmt.Printf("%s %s has salary $%d", e.firstName, e.lastName, (e.basicPay + e.pf)) } func (e Employee) CalculateLeavesLeft() int { return e.totalLeaves - e.leavesTaken }