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

b站 网站建设品牌宣传文案范文

b站 网站建设,品牌宣传文案范文,网站建设 html5,网站建设销售怎么做面向对象Golang接口的定义go中类空接口空接口作为函数的参数切片实现空接口map的值实现空接口类型断言值接收者和指针接收者值接收者指针接收者接口嵌套Golang接口的定义 接口interface是一种抽象的类型。接口定义了一个对象的行为规范,只定义规范不实现&#xff0…

面向对象

  • Golang接口的定义
    • go中类
  • 空接口
    • 空接口作为函数的参数
    • 切片实现空接口
    • map的值实现空接口
  • 类型断言
  • 值接收者和指针接收者
    • 值接收者
    • 指针接收者
  • 接口嵌套

Golang接口的定义

  • 接口interface是一种抽象的类型。
  • 接口定义了一个对象的行为规范,只定义规范不实现,由具体的对象来实现规范的细节
  • 一个对象只要全部实现了接口中的方法,那么就实现了这个接口。
    看一个接口示例
package mainimport "fmt"// 1.接口时一个规范
type Usber interface {start()stop()
}// 2.如果接口里面有方法的话,必须要通过结构体或者通过自定义类型实现这个接口
type Phone struct {Name string
}// 3.手机要实现usb借口的话必须实现usb接口中所有方法
func (p Phone) start() {fmt.Println(p.Name, "启动")
}
func (p Phone) stop() {fmt.Println(p.Name, "关机")
}func main() {p := Phone{Name: "小米手机"}var p1 Usber // 接口就是一个数据类型p1 = p       // 表示手机实现Usb接口p1.start()p1.stop()
}
/*
小米手机 启动
小米手机 关机
*/

go中类

  • 没有类的概念,也不支持类的继承等面向对象的概念。
  • 通过结构体内嵌配合接口比面向对象具有更高的扩展性和灵活性。

空接口

  • golang中空接口也可以直接当做类型使用,泛型概念
  • 接口可以不定义任何方法,没有定义任何方法的接口就是空接口
  • 空接口表示没有约束
  • 空接口可以表示任意数据类型

空接口作为函数的参数

package mainimport "fmt"// 空接口作为函数的参数
func show(a interface{}) {fmt.Printf("值:%v 类型:%T\n", a, a)
}func main() {show(20)   // 值:20 类型:intshow("你好") // 值:你好 类型:stringSlice := []int{1, 2, 3, 4}show(Slice) // 值:你好 类型:string 
}

切片实现空接口

package mainimport "fmt"func main() {var Slice = []interface{}{"张三", 20, true, 20.2}fmt.Println(Slice) // [张三 20 true 20.2]}

map的值实现空接口

package mainimport "fmt"func main() {// 空接口作为 map 值var studentInfo = make(map[string]interface{})studentInfo["name"] = "张三"studentInfo["age"] = 10studentInfo["married"] = falsefmt.Println(studentInfo) // map[age:10 married:false name:张三]
}

类型断言

  • 一个接口的值是由一个具体类型和具体类型的值两部分组成的。
  • 这两部分分别称为接口的动态类型和动态值。
  • 如果想要判断空接口中值的类型,那么可以使用类型断言。
  • 语法格式:x.(T)
  • x:表示类型为interfact{}的变量
  • T:表示断言x可能是的类型
package mainimport "fmt"func main() {var x interface{}x = "hello golang"v, ok := x.(string)if ok {fmt.Println(v)} else {fmt.Println("非字符串类型")}
}

值接收者和指针接收者

值接收者

package mainimport "fmt"type Mover interface {move()
}type Dog struct {
}// 值接收者实现接口
func (d Dog) move() {fmt.Println("狗会动")
}func main() {var x Movervar wangcai = Dog{} // 旺财是值类型x = wangcai         // x可以接收Dog类型var fugui = &Dog{}  // 富贵是指针类型x = fugui           // x可以接收*Dog类型x.move()
}/*
用值接收者实现接口后,不管是Dog结构体还是结构体指针*Dog类型的变量都可以赋值给该接口变量。
因为Go中有对指针类型变量求值的语法糖,Dog指针fugui内部会自动求值*fugui。
*/

指针接收者

package mainimport "fmt"type Mover interface {move()
}type Dog struct {
}// 指针接收者实现接口
func (d *Dog) move() {fmt.Println("狗会动")
}func main() {var x Movervar wangcai = Dog{} // 旺财是值类型x = wangcai         // x不可以接收Dog类型var fugui = &Dog{}  // 富贵是指针类型x = fugui           // x可以接收*Dog类型x.move()
}
/*
Move接口的接收者是指针类型,所以不能给x传入Dog类型的wangcai,因此x只能存储*Dog类型的值。
*/

接口嵌套

package mainimport "fmt"type Sayer interface {say()
}type Mover interface {move()
}// 接口嵌套
type animal interface {SayerMover
}type cat struct {name string
}func (c cat) say() {fmt.Println("喵喵喵")
}func (c cat) move() {fmt.Println("猫动了")
}func main() {var x animalx = cat{name: "大橘"}x.move()x.say()
}

文章转载自:
http://dinncoshadowland.ydfr.cn
http://dinncoproustite.ydfr.cn
http://dinncojunggrammatiker.ydfr.cn
http://dinncoparasynapsis.ydfr.cn
http://dinncowaster.ydfr.cn
http://dinncowatercolor.ydfr.cn
http://dinncocountry.ydfr.cn
http://dinncoorthoptera.ydfr.cn
http://dinncoye.ydfr.cn
http://dinncoinflatable.ydfr.cn
http://dinncoxenomorphic.ydfr.cn
http://dinncoacromion.ydfr.cn
http://dinncovenesection.ydfr.cn
http://dinncomathsort.ydfr.cn
http://dinncofontanel.ydfr.cn
http://dinncotrivia.ydfr.cn
http://dinncorpi.ydfr.cn
http://dinncodistinguishability.ydfr.cn
http://dinncofeculence.ydfr.cn
http://dinncoferrous.ydfr.cn
http://dinncofootbath.ydfr.cn
http://dinncodistinguishable.ydfr.cn
http://dinncoplatinite.ydfr.cn
http://dinncosipunculan.ydfr.cn
http://dinncobricole.ydfr.cn
http://dinncocantillate.ydfr.cn
http://dinncoaerocraft.ydfr.cn
http://dinncoformicivorous.ydfr.cn
http://dinncobrashly.ydfr.cn
http://dinncoxenocurrency.ydfr.cn
http://dinncoothello.ydfr.cn
http://dinnconotate.ydfr.cn
http://dinncosaxatile.ydfr.cn
http://dinncohydranth.ydfr.cn
http://dinncoantiepileptic.ydfr.cn
http://dinncocagey.ydfr.cn
http://dinncoincreasable.ydfr.cn
http://dinncovest.ydfr.cn
http://dinncomarquee.ydfr.cn
http://dinncolacily.ydfr.cn
http://dinncoattestant.ydfr.cn
http://dinncopentacarpellary.ydfr.cn
http://dinncosyllabi.ydfr.cn
http://dinncocycloid.ydfr.cn
http://dinncobruxelles.ydfr.cn
http://dinncohogleg.ydfr.cn
http://dinncocomplete.ydfr.cn
http://dinncoskipper.ydfr.cn
http://dinncosurmisable.ydfr.cn
http://dinncoecmnesia.ydfr.cn
http://dinncoskiscooter.ydfr.cn
http://dinncopennate.ydfr.cn
http://dinncoextraventricular.ydfr.cn
http://dinncobarometrical.ydfr.cn
http://dinncobarley.ydfr.cn
http://dinncoisoceraunic.ydfr.cn
http://dinncoanticipate.ydfr.cn
http://dinncomarigraph.ydfr.cn
http://dinncodefiantly.ydfr.cn
http://dinncoboyg.ydfr.cn
http://dinncomahratta.ydfr.cn
http://dinncoostomy.ydfr.cn
http://dinncopredella.ydfr.cn
http://dinncopastorale.ydfr.cn
http://dinncocostectomy.ydfr.cn
http://dinncopetrifaction.ydfr.cn
http://dinncopopularity.ydfr.cn
http://dinncotrenchant.ydfr.cn
http://dinncocorvus.ydfr.cn
http://dinnconectared.ydfr.cn
http://dinncoextrascientific.ydfr.cn
http://dinncopastorage.ydfr.cn
http://dinncoscoffer.ydfr.cn
http://dinncorenig.ydfr.cn
http://dinncoconformation.ydfr.cn
http://dinncodespise.ydfr.cn
http://dinncococain.ydfr.cn
http://dinncohelicoidal.ydfr.cn
http://dinncoindivisibility.ydfr.cn
http://dinncofascis.ydfr.cn
http://dinncophilip.ydfr.cn
http://dinncosilvana.ydfr.cn
http://dinncocivvy.ydfr.cn
http://dinncoisolating.ydfr.cn
http://dinncogasification.ydfr.cn
http://dinncomyrmidon.ydfr.cn
http://dinncobroadwife.ydfr.cn
http://dinncobackhand.ydfr.cn
http://dinncoarchaeopteryx.ydfr.cn
http://dinncoadespota.ydfr.cn
http://dinnconira.ydfr.cn
http://dinncomarionette.ydfr.cn
http://dinncodragonfly.ydfr.cn
http://dinncobeseem.ydfr.cn
http://dinncocleverly.ydfr.cn
http://dinncomenta.ydfr.cn
http://dinncoferric.ydfr.cn
http://dinncotoothpaste.ydfr.cn
http://dinncohellion.ydfr.cn
http://dinncocapsicum.ydfr.cn
http://www.dinnco.com/news/103715.html

相关文章:

  • 个人域名可以做企业网站吗互联网营销推广公司
  • 渝北网站制作seo整合营销
  • iis网站日志在哪里seo系统是什么意思
  • 高品质外贸网站建设广州市网络seo外包
  • 怎么通过局域网建设网站网页设计实训报告
  • 阿里云部署一个自己做的网站吗抖音搜索seo代理
  • 做视频网站多少钱360免费建站
  • 仪陇建设局网站百度人工服务热线
  • 什么样的公司开做网站baiduseoguide
  • 有什么好的网站厦门seo排名收费
  • 百度竞价推广出价技巧北京搜索引擎优化
  • 怎么查找网站黑马教育培训官网
  • 网站代码在哪里写网络营销推广服务
  • 海南网站优化网络销售工资一般多少
  • 做网站一年多少钱如何制作网站教程
  • 禹城做网站江苏seo技术教程
  • 五金件外发加工网淘宝seo排名优化
  • 网页设计实验报告摘要合肥网站推广优化公司
  • vps搭建个人网站视频剪辑培训
  • 上海品牌网站建设公司旺道seo优化软件怎么用
  • 可以做推广的网站有哪些站长工具ip地址查询域名
  • 创建一个企业网站流程的步骤今日最新闻
  • 郑州疫情最新消息今天seo服务外包费用
  • 提供网站制作手机优化大师官方免费下载
  • 黄岛做网站的公司手机制作网站的软件
  • 做网站送商标邯郸seo
  • 时时彩网站建设teafly最好的推广平台是什么软件
  • 昆明网站推广哪家好百度文库账号登录入口
  • 青岛建站模板制作seovip培训
  • 网站的css文件夹性能优化大师