Go语言接口与汉堡店应用开发全解析
1. Go语言接口类型
1.1 静态与动态接口类型
在Go语言中,接口有静态和动态两种类型。静态类型就是接口本身的类型,例如 AccountOperations 接口的静态类型就是 AccountOperations 。动态类型则是实现该接口的类型。
接口在内部由一个元组表示,该元组代表接口的动态类型和该动态类型的值。以下代码展示了接口的内部表示:
package main
import "fmt"
type AccountOperations interface{
// Methods
computeInterest() float64
}
type SavingsAccount struct {
number string
balance float64
}
type CheckingAccount struct {
number string
balance float64
}
func(a *SavingsAccount) computeInterest() float64{
return 0.005
}
func(a *CheckingAccount) computeInterest() float64{
return 0.001
}
func describe(ao AccountOperations) {
// we use %T to display the dynamic type of ao
/
超级会员免费看
订阅专栏 解锁全文
1026

被折叠的 条评论
为什么被折叠?



