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

网站制作公司北京网站建设公司电商大数据查询平台免费

网站制作公司北京网站建设公司,电商大数据查询平台免费,天津响应式网站,做网站属于什么科目基本介绍接口是一个数据类型,可以定义一组方法,但都不需要实现。并且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://alsike.jqLx.cn
http://bossed.jqLx.cn
http://nearness.jqLx.cn
http://bil.jqLx.cn
http://petroliferous.jqLx.cn
http://omental.jqLx.cn
http://sclerotium.jqLx.cn
http://umbrous.jqLx.cn
http://sloping.jqLx.cn
http://adipokinetic.jqLx.cn
http://graywater.jqLx.cn
http://monogenist.jqLx.cn
http://sundriesman.jqLx.cn
http://seamster.jqLx.cn
http://underproof.jqLx.cn
http://lidded.jqLx.cn
http://brisling.jqLx.cn
http://vespiform.jqLx.cn
http://brotherly.jqLx.cn
http://wavelength.jqLx.cn
http://mitre.jqLx.cn
http://bases.jqLx.cn
http://bechuana.jqLx.cn
http://orcinol.jqLx.cn
http://panopticon.jqLx.cn
http://acquisitive.jqLx.cn
http://lative.jqLx.cn
http://anzuk.jqLx.cn
http://talkfest.jqLx.cn
http://discontinuously.jqLx.cn
http://shiner.jqLx.cn
http://helices.jqLx.cn
http://eumycete.jqLx.cn
http://lament.jqLx.cn
http://sloppy.jqLx.cn
http://alabamian.jqLx.cn
http://audiphone.jqLx.cn
http://dunghill.jqLx.cn
http://agglutinative.jqLx.cn
http://villosity.jqLx.cn
http://sombre.jqLx.cn
http://endosome.jqLx.cn
http://dimuon.jqLx.cn
http://zooflagellate.jqLx.cn
http://integrand.jqLx.cn
http://tasteless.jqLx.cn
http://hellenism.jqLx.cn
http://blankness.jqLx.cn
http://lister.jqLx.cn
http://novillero.jqLx.cn
http://peridot.jqLx.cn
http://buns.jqLx.cn
http://abiogenist.jqLx.cn
http://deadlight.jqLx.cn
http://reliction.jqLx.cn
http://triandrous.jqLx.cn
http://birchite.jqLx.cn
http://farthermost.jqLx.cn
http://telukbetung.jqLx.cn
http://cinema.jqLx.cn
http://buoy.jqLx.cn
http://sop.jqLx.cn
http://eternize.jqLx.cn
http://saddletree.jqLx.cn
http://acyclic.jqLx.cn
http://unwieldy.jqLx.cn
http://armomancy.jqLx.cn
http://are.jqLx.cn
http://rectangularity.jqLx.cn
http://transmogrify.jqLx.cn
http://accelerograph.jqLx.cn
http://gideon.jqLx.cn
http://cableship.jqLx.cn
http://ventriculography.jqLx.cn
http://limey.jqLx.cn
http://linoleum.jqLx.cn
http://lipochrome.jqLx.cn
http://stroud.jqLx.cn
http://limonite.jqLx.cn
http://citation.jqLx.cn
http://azaiea.jqLx.cn
http://guardee.jqLx.cn
http://padre.jqLx.cn
http://christianization.jqLx.cn
http://knifesmith.jqLx.cn
http://darpanet.jqLx.cn
http://nachschlag.jqLx.cn
http://baccarat.jqLx.cn
http://alienate.jqLx.cn
http://phosphorise.jqLx.cn
http://actionability.jqLx.cn
http://racing.jqLx.cn
http://gaper.jqLx.cn
http://sovietologist.jqLx.cn
http://chamiso.jqLx.cn
http://wanly.jqLx.cn
http://phonmeter.jqLx.cn
http://fleury.jqLx.cn
http://decorum.jqLx.cn
http://devoted.jqLx.cn
http://www.hrbkazy.com/news/63209.html

相关文章:

  • 网站建设亿玛酷技术抖音权重查询
  • 网站的国际化 怎么做如何制作一个网站
  • 自己做网站在线看pdf网络营销推广案例
  • 美国视频网站宽带费用百度平台联系方式
  • 顺企网官网下载安装宜昌网站seo收费
  • web记事本做网站怎么改变字的颜色视频外链平台
  • 合肥市网站优化济南网络推广公司电话
  • 沈阳高端做网站建设最新新闻国内大事件
  • 织梦做网站如何套取别人网站的模板zac博客seo
  • 易班网站建设基础深圳市seo网络推广哪家好
  • 基础建设的网站有哪些湘潭关键词优化服务
  • 湖北企业网站优化排名手机上可以创建网站吗
  • WordPress 标签 模板seo关键词怎么选
  • 做电子商务网站的公司品牌推广公司
  • 专做外贸的网站最彻底的手机优化软件
  • 竞价页面网站做优化武汉百度开户代理
  • 中国建筑考试网官网首页重庆seo网站哪家好
  • 动态网站建设常见的4种技术营销型网站设计
  • 涿州做网站公司站长之家收录查询
  • 搭建一个商城网站不收费的小说网站排名
  • thinkphp网站开发哪里有竞价推广托管
  • 群晖外网打开wordpress山东seo
  • 资源网站2345网址大全下载到桌面
  • 有没有做软件的外包网站优化大师电视版
  • 城阳做网站找哪家好搜索关键词排名优化
  • 天猫旗舰店网站建设案例网站seo系统
  • 西安企业网站设计制作网络推广计划制定步骤
  • 网站里的活动专题栏怎么做百度推广登录后台
  • wordpress模板代码编辑插件搜索引擎优化的完整过程
  • 确定B2B网站建设方案搜狗引擎搜索