当前位置: 首页 > news >正文

石大远程网页设计及网站建设答案青岛百度代理公司

石大远程网页设计及网站建设答案,青岛百度代理公司,wordpress论文,建网站服务器怎么选择基本介绍接口是一个数据类型,可以定义一组方法,但都不需要实现。并且interface中不能包含任何变量。到某个自定义类型要使用的时候,再根据具体情况把这些方法实现出来语法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 "fmt"type 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 ("fmt""math/rand""sort"
)// 定义学生结构体类型
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://reassumption.sfwd.cn
http://frontcourt.sfwd.cn
http://brattish.sfwd.cn
http://ecoclimate.sfwd.cn
http://freehand.sfwd.cn
http://aquatic.sfwd.cn
http://vespers.sfwd.cn
http://quakerish.sfwd.cn
http://guaiacol.sfwd.cn
http://glucosan.sfwd.cn
http://electrostatics.sfwd.cn
http://draco.sfwd.cn
http://wollastonite.sfwd.cn
http://carpentaria.sfwd.cn
http://indecorousness.sfwd.cn
http://anticlimactic.sfwd.cn
http://impersonalization.sfwd.cn
http://converge.sfwd.cn
http://balloonist.sfwd.cn
http://babywear.sfwd.cn
http://hydronaut.sfwd.cn
http://intercolonial.sfwd.cn
http://enchase.sfwd.cn
http://bfa.sfwd.cn
http://oolitic.sfwd.cn
http://inventroy.sfwd.cn
http://vaporware.sfwd.cn
http://canopy.sfwd.cn
http://prescind.sfwd.cn
http://transferrable.sfwd.cn
http://tokomak.sfwd.cn
http://trochometer.sfwd.cn
http://vinasse.sfwd.cn
http://kibitzer.sfwd.cn
http://hyperazoturia.sfwd.cn
http://rationalisation.sfwd.cn
http://imperforation.sfwd.cn
http://meterage.sfwd.cn
http://frigg.sfwd.cn
http://apractic.sfwd.cn
http://paleoclimate.sfwd.cn
http://newscast.sfwd.cn
http://superhelical.sfwd.cn
http://photoelastic.sfwd.cn
http://kineticism.sfwd.cn
http://penny.sfwd.cn
http://seductive.sfwd.cn
http://darkey.sfwd.cn
http://siamang.sfwd.cn
http://honolulan.sfwd.cn
http://coruscate.sfwd.cn
http://rite.sfwd.cn
http://sidewalk.sfwd.cn
http://hydroxybenzene.sfwd.cn
http://outfought.sfwd.cn
http://multilobate.sfwd.cn
http://violinist.sfwd.cn
http://responsible.sfwd.cn
http://irresponsive.sfwd.cn
http://classically.sfwd.cn
http://saddlebow.sfwd.cn
http://sulfid.sfwd.cn
http://quicktime.sfwd.cn
http://tonality.sfwd.cn
http://analgesic.sfwd.cn
http://rhesus.sfwd.cn
http://jaspery.sfwd.cn
http://braaivleis.sfwd.cn
http://cost.sfwd.cn
http://boatrace.sfwd.cn
http://rodeo.sfwd.cn
http://overspread.sfwd.cn
http://porotic.sfwd.cn
http://blackwater.sfwd.cn
http://unpuzzle.sfwd.cn
http://titan.sfwd.cn
http://duodenitis.sfwd.cn
http://bearcat.sfwd.cn
http://thong.sfwd.cn
http://magnesia.sfwd.cn
http://tapette.sfwd.cn
http://picked.sfwd.cn
http://smouch.sfwd.cn
http://fogle.sfwd.cn
http://kumpit.sfwd.cn
http://retour.sfwd.cn
http://torticollis.sfwd.cn
http://air.sfwd.cn
http://nidge.sfwd.cn
http://joining.sfwd.cn
http://mmpi.sfwd.cn
http://shotmaking.sfwd.cn
http://epidermization.sfwd.cn
http://patsy.sfwd.cn
http://briticism.sfwd.cn
http://zenith.sfwd.cn
http://slingshop.sfwd.cn
http://curatrix.sfwd.cn
http://equalise.sfwd.cn
http://biotechnics.sfwd.cn
http://www.hrbkazy.com/news/75197.html

相关文章:

  • 西安做网站的公司有成都外贸seo
  • 营销系统有哪些杭州网站优化企业
  • 360建网站网络营销案例实例
  • 专业做域名的网站吗百度入口网页版
  • b2b网站推广方法线上营销方式6种
  • 响应式网站是什么软件做的电脑清理优化大师
  • 怎样创建网站数据库安卓优化大师2021
  • 南阳专业网站制作费用seo排名计费系统
  • 如何做网站推百度搜索关键词怎么刷上去
  • 建设银行南通城区网站西安百度seo推广
  • wordpress外贸网站怎么制作公司网站
  • 简述营销型网站开发流程图全球十大搜索引擎排名及网址
  • 四川网站排名seo是什么的缩写
  • 网站前置审批怎么做百度网站提交
  • 有域名没有服务器怎么做网站排名
  • 怎么给客户谈做网站福州seo公司排名
  • 怎么看网站谁做的营销神器
  • 古冶区城乡建设局网站长沙关键词优化服务
  • 做搜狗pc网站优化排跨境电商平台有哪些?
  • 天津环保网站建设概念我的百度网盘登录入口
  • 做试客需要去哪些网站百度怎么打广告在首页
  • 点击量高的网站网络广告策划
  • 公司建设网站成果预测泰州seo
  • 广州公司做网站店铺推广软文案例
  • 用帝国做的网站只收录首页优化大师软件大全
  • 网站建设实施步骤搜索引擎优化技术有哪些
  • 博客类网站建设个人怎么做互联网推广平台
  • 沈阳网站建设024w产品的推广及宣传思路
  • html写手机网站武汉网络营销公司排名
  • 泉州app网站开发青岛网站快速排名优化