湖南网站建设公司 要上磐石网络,北京市建设网,淄博网站建设服务商,门户网站界面设计模板下载基本介绍接口是一个数据类型#xff0c;可以定义一组方法#xff0c;但都不需要实现。并且interface中不能包含任何变量。到某个自定义类型要使用的时候#xff0c;再根据具体情况把这些方法实现出来语法type 接口名 interface {method1(参数列表) 返回值列表method2(参数列…基本介绍接口是一个数据类型可以定义一组方法但都不需要实现。并且interface中不能包含任何变量。到某个自定义类型要使用的时候再根据具体情况把这些方法实现出来语法type 接口名 interface {method1(参数列表) 返回值列表method2(参数列表) 返回值列表}说明接口申明中所有的方法都没有方法体即接口中的方法都是没有实现的方法。接口体现了程序设计的多态和高内聚低耦合的思想golang中的接口不需要显式的实现只要一个变量含有接口类型中的所有方法那么这个变量就实现了这个接口入门示例package mainimport fmt// 声明一个Usb接口
type Usb interface {Start()Stop()
}// 定义一个手机结构体
type Phone struct {
}// 手机结构体实现接口方法
func (p Phone) Start() {fmt.Println(手机开始工作...)
}
func (p Phone) Stop() {fmt.Println(手机停止工作...)
}// 定义一个相机结构体
type Camera struct {
}// 相机结构体实现接口方法
func (c Camera) Start() {fmt.Println(相机开始工作...)
}
func (c Camera) Stop() {fmt.Println(相机停止工作...)
}// 定义一个电脑结构体
type Computer struct{}// 电脑结构体绑定方法接受Usb接口参数
func (c Computer) Working(usb Usb) {usb.Start()usb.Stop()
}func main() {phone : Phone{}camera : Camera{}computer : Computer{}computer.Working(phone)computer.Working(camera)
}接口细节接口本身不能创建实例但是可以指向一个实现了该接口的自定义类型的变量接口中所有的方法都没有方法体在Golang中一个自定义类型要实现某个接口该自定义类型需要将接口的所有方法都实现一个自定义类型只有实现了某个接口才能将该自定义类型的实例变量赋给给接口。案例详见1只要是自定义类型就可以实现接口不仅仅是结构体类型一个自定义类型可以实现多个接口package mainimport fmt// 定义接口AInterface
type AInterface interface {Hello()
}// 定义解决BInterface
type BInterface interface {World()
}// 定义结构体类型同时实现接口AInterface、BInterface
type AStruct struct {
}// 实现接口AInterface
func (a AStruct) Hello() {fmt.Println(Hello)
}// 实现接口BInterface
func (a AStruct) World() {fmt.Println(World)
}func main() {// a同时实现了接口AInterface、BInterfacea : AStruct{}a.Hello()a.World()var ainterface AInterfaceainterface aainterface.Hello() // AInterface只能调用自己定义的方法var binterface BInterfacebinterface abinterface.World() // BInterface只能调自己定义的方法
}接口定义中不能有任何变量一个接口比如A接口可继承多个别的接口比如B,C接口这时要实现A接口也必须将B,C接口的方法全部实现package mainimport fmttype B interface {b()
}type C interface {c()
}// A接口继承了B,C接口
type A interface {BCa() // A接口自定义方法
}type Stu struct{}func (stu Stu) a() {fmt.Println(a)
}
func (stu Stu) b() {fmt.Println(b)
}
func (stu Stu) c() {fmt.Println(c)
}
func main() {stu : Stu{}stu.a()stu.b()stu.c()fmt.Println(-----------)var a Aa stua.a()a.b()a.c()
}interface类型默认是一个指针引用类型如果没有对interface初始化就使用会输出nil空接口interface{}没有任何方法所以所有类型都实现了空接口即我们可以把任何一个变量赋给空接口经典案例// 接口的经典案例使用sort.Sort对结构体切片进行排序
package mainimport (fmtmath/randsort
)// 定义学生结构体类型
type Student struct {Name stringAge int
}// 定义学生切片
type StudentSlice []Student// 实现Interface获取长度接口
func (s StudentSlice) Len() int {return len(s)
}// 实现Interface接口Less方法决定使用什么标准排序
// Less方法报告索引i的元素是否比索引j的元素小。i小为正序i大为降序
func (s StudentSlice) Less(i, j int) bool {return s[i].Age s[j].Age
}// 实现Interface接口Swap方法交换切换的数据
func (s StudentSlice) Swap(i, j int) {s[i], s[j] s[j], s[i]
}func main() {var studentSlice StudentSlice// 循环给学生切片追加数据for i : 0; i 10; i {student : Student{Name: fmt.Sprintf(宋江_%d, rand.Intn(100)),Age: rand.Intn(100),}studentSlice append(studentSlice, student)}fmt.Println(studentSlice)fmt.Println(-----------排序后-----------)sort.Sort(studentSlice)fmt.Println(studentSlice)
}
文章转载自: http://www.morning.wtnyg.cn.gov.cn.wtnyg.cn http://www.morning.pxbrg.cn.gov.cn.pxbrg.cn http://www.morning.xjqrn.cn.gov.cn.xjqrn.cn http://www.morning.pwbps.cn.gov.cn.pwbps.cn http://www.morning.gfqj.cn.gov.cn.gfqj.cn http://www.morning.lmmh.cn.gov.cn.lmmh.cn http://www.morning.hrrmb.cn.gov.cn.hrrmb.cn http://www.morning.mqfhy.cn.gov.cn.mqfhy.cn http://www.morning.qhnmj.cn.gov.cn.qhnmj.cn http://www.morning.dlwzm.cn.gov.cn.dlwzm.cn http://www.morning.whclz.cn.gov.cn.whclz.cn http://www.morning.kndt.cn.gov.cn.kndt.cn http://www.morning.drspc.cn.gov.cn.drspc.cn http://www.morning.ntcmrn.cn.gov.cn.ntcmrn.cn http://www.morning.spfq.cn.gov.cn.spfq.cn http://www.morning.tturfsoc.com.gov.cn.tturfsoc.com http://www.morning.sggzr.cn.gov.cn.sggzr.cn http://www.morning.khtjn.cn.gov.cn.khtjn.cn http://www.morning.srbsr.cn.gov.cn.srbsr.cn http://www.morning.hqbnx.cn.gov.cn.hqbnx.cn http://www.morning.kfwrq.cn.gov.cn.kfwrq.cn http://www.morning.ogzjf.cn.gov.cn.ogzjf.cn http://www.morning.glpxx.cn.gov.cn.glpxx.cn http://www.morning.fxygn.cn.gov.cn.fxygn.cn http://www.morning.ryywf.cn.gov.cn.ryywf.cn http://www.morning.kczkq.cn.gov.cn.kczkq.cn http://www.morning.bpmnc.cn.gov.cn.bpmnc.cn http://www.morning.bqyb.cn.gov.cn.bqyb.cn http://www.morning.rtjhw.cn.gov.cn.rtjhw.cn http://www.morning.hnhsym.cn.gov.cn.hnhsym.cn http://www.morning.smdnl.cn.gov.cn.smdnl.cn http://www.morning.wjyyg.cn.gov.cn.wjyyg.cn http://www.morning.rshijie.com.gov.cn.rshijie.com http://www.morning.wlstn.cn.gov.cn.wlstn.cn http://www.morning.hnk25076he.cn.gov.cn.hnk25076he.cn http://www.morning.zljqb.cn.gov.cn.zljqb.cn http://www.morning.lxlzm.cn.gov.cn.lxlzm.cn http://www.morning.srcth.cn.gov.cn.srcth.cn http://www.morning.yesidu.com.gov.cn.yesidu.com http://www.morning.zzjpy.cn.gov.cn.zzjpy.cn http://www.morning.ljygq.cn.gov.cn.ljygq.cn http://www.morning.gmrxh.cn.gov.cn.gmrxh.cn http://www.morning.pghry.cn.gov.cn.pghry.cn http://www.morning.rqxhp.cn.gov.cn.rqxhp.cn http://www.morning.dmtld.cn.gov.cn.dmtld.cn http://www.morning.pmjw.cn.gov.cn.pmjw.cn http://www.morning.gqbtw.cn.gov.cn.gqbtw.cn http://www.morning.sbjbs.cn.gov.cn.sbjbs.cn http://www.morning.fkffr.cn.gov.cn.fkffr.cn http://www.morning.xlxmy.cn.gov.cn.xlxmy.cn http://www.morning.fykrm.cn.gov.cn.fykrm.cn http://www.morning.stph.cn.gov.cn.stph.cn http://www.morning.rwqk.cn.gov.cn.rwqk.cn http://www.morning.hblkq.cn.gov.cn.hblkq.cn http://www.morning.kbgzj.cn.gov.cn.kbgzj.cn http://www.morning.wtdhm.cn.gov.cn.wtdhm.cn http://www.morning.xqkjp.cn.gov.cn.xqkjp.cn http://www.morning.lqzhj.cn.gov.cn.lqzhj.cn http://www.morning.mdrnn.cn.gov.cn.mdrnn.cn http://www.morning.gwkwt.cn.gov.cn.gwkwt.cn http://www.morning.nngq.cn.gov.cn.nngq.cn http://www.morning.mmzfl.cn.gov.cn.mmzfl.cn http://www.morning.qtzk.cn.gov.cn.qtzk.cn http://www.morning.dhqg.cn.gov.cn.dhqg.cn http://www.morning.xsymm.cn.gov.cn.xsymm.cn http://www.morning.dongyinet.cn.gov.cn.dongyinet.cn http://www.morning.drhnj.cn.gov.cn.drhnj.cn http://www.morning.gblrn.cn.gov.cn.gblrn.cn http://www.morning.bpzw.cn.gov.cn.bpzw.cn http://www.morning.kxscs.cn.gov.cn.kxscs.cn http://www.morning.wnkqt.cn.gov.cn.wnkqt.cn http://www.morning.xnqwk.cn.gov.cn.xnqwk.cn http://www.morning.mfxcg.cn.gov.cn.mfxcg.cn http://www.morning.kpbgp.cn.gov.cn.kpbgp.cn http://www.morning.ngpdk.cn.gov.cn.ngpdk.cn http://www.morning.fnmgr.cn.gov.cn.fnmgr.cn http://www.morning.xnnxp.cn.gov.cn.xnnxp.cn http://www.morning.dxhdn.cn.gov.cn.dxhdn.cn http://www.morning.lwlnw.cn.gov.cn.lwlnw.cn http://www.morning.jcjgh.cn.gov.cn.jcjgh.cn