Go基础知识学习(6) 接口
Golang接口定义使用interface来声明,它相对于其他语言最大的特定就是接口定义和实现的关联性仅仅依赖接口的名字和声明,无需显式声明。
Code: package main import ( "fmt" ) type city struct { name string } func (c city) Put(name string) { c.name = name } func (c city) GetName() string { return c.name } type country struct { name string } func (c country) Put(name string) { c.name = name } func (c country) GetName() string { return c.name } type IName interface { Put(string) GetName() string } //interface type and query func printname(p interface{}) { ivalue,ok := p.(IName) if !ok { fmt.Println("It is not a IName interface obj:",p) return } switch ivalue.(type) { case *city: fmt.Println("It is a *city: ",ivalue.GetName()) case *country: fmt.Println("It is a *country: ",ivalue.GetName()) case city: fmt.Println("It is a city: ",ivalue.GetName()) case country: fmt.Println("It is a country: ",ivalue.GetName()) default: fmt.Println("It is other IName interface") } } func main() { var c1,c2,c3,c4 interface{} c1 = city{name: "Hangzhou"} c2 = country{name: "US"} c3 = &city{name: "Shanghai"} c4 = &country{name: "Japan"} fmt.Println(c1) fmt.Println(c2) fmt.Println(c3) fmt.Println(c4) //print name of object has interface IName printname(c1) printname(c2) printname(c3) printname(c4) //print name of object not has interface IName printname(10) printname("abc") } Output: {Hangzhou} {US} &{Shanghai} &{Japan} It is a city: Hangzhou It is a country: US It is a *city: Shanghai It is a *country: Japan It is not a IName interface obj: 10 It is not a IName interface obj: abc code : https://github.com/panyingyun/gostudy/blob/master/exp8.go (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |