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

南充建设机械网站黄村专业网站开发公司

南充建设机械网站,黄村专业网站开发公司,汕头建站服务,别墅效果图制作接口 接口用于定义对象的行为#xff0c;接口只指定对象应该做什么#xff0c;实现这种行为由对象来决定。接口只是把所有具有共性的方法定义在一起#xff0c;任何类型实现了接口中所有的方法#xff0c;就是实现了这个接口。接口存在的意义就是用来定义规范#xff0c;…接口 接口用于定义对象的行为接口只指定对象应该做什么实现这种行为由对象来决定。接口只是把所有具有共性的方法定义在一起任何类型实现了接口中所有的方法就是实现了这个接口。接口存在的意义就是用来定义规范用来做功能的拓展好处是可以实现多态在Go中只要接口中的方法和结构体中的一样就会认为这么结构体实现了这个接口的方法接口是引用类型传值的时候是以引用方式地址传送进去的 接口定义及调用 定义格式 tepe 接口名 interface{方法名(参数) 返回值 }调用格式1 对象名.方法名调用格式2这种方式必须实现接口中声明的所有方法 通过接口变量调用在代码中是体现不出来如何实现接口的因为结构体和接口看似是没有任何关联的其实Go中只要接口中的方法和结构体中的一样就会认为这么结构体实现了这个接口的方法 type Student struct{}type Teacher struct{}type TestInterface interface {//age int // 接口中不能有变量//Welcome()Hello() }type TestInterface2 interface {Hello2() }// 一个结构体可以实现多个接口 func (student *Student) Hello() {fmt.Println(Student实现了接口的Hello方法) }func (student *Student) Hello2() {fmt.Println(Student实现了接口的Hello2方法) }func (teacher *Teacher) Hello() {fmt.Println(Teacher实现了接口的Hello方法) }func main() {// 对象名.方法名var stu Studentstu.Hello()var tea Teachertea.Hello()// 通过接口变量调用如果接口中有两个方法但是只实现了一个就会报错必须全部实现var testInterface TestInterfacetestInterface stutestInterface.Hello()testInterface teatestInterface.Hello() }输出 Student实现了接口的Hello方法 Teacher实现了接口的Hello方法 Student实现了接口的Hello方法 Teacher实现了接口的Hello方法多态 多态是同一个接口使用不同的实例执行不同的操作多态是除封装、继承之后面向对象的第三大特征。多态是出现在接口关系中的(只能是接口)举例win电脑和Mac都是电脑接口但是mac只能办公用win可以办公可以玩游戏不同类型的电脑具有不同的特征就是多态 多态的实现 func 函数名 (参数 接口类型){}演示 func main() {var stu Studentvar tea Teacher// 多态要加取地址符因为方法是指针类型Polymorphic(stu) //Student类实现了接口的Hello方法Polymorphic(tea) // Teacher类实现了接口的Hello方法 }type Student struct{}type Teacher struct{}type TestInterface interface {Hello() }func (student *Student) Hello() {fmt.Println(Student类实现了接口的Hello方法) }func (teacher *Teacher) Hello() {fmt.Println(Teacher类实现了接口的Hello方法) }func Polymorphic(polymorphic TestInterface) {polymorphic.Hello() }接口的继承与转换 接口的继承和普通继承是一样的直接写接口名就可以了但是要实现继承接口的所有方法 func main() {var stu Studentvar testInterface02 TestInterface02// 接口中的方法数据类型是指针类型就要使用取地址符如果方法的类型不是指针就不用加testInterface02 stutestInterface02.Hello() // 可以调用所继承接口中的方法testInterface02.HelloHello()var test TestInterfacetest testInterface02test.Hello() }type Student struct{}type TestInterface interface {Hello() }type TestInterface02 interface {TestInterface // 继承接口HelloHello() }func (student *Student) Hello() {fmt.Println(Hello) }func (student *Student) HelloHello() {fmt.Println(HelloHello) }空接口定义及使用 空接口(interface {})不包含任何的方法正因为如此所有的类型都实现了空接口因此空接口可以存储任意类型的数值 func main() {// 定义空接口的切片由于是空接口所以可以存储任意类型的数据var s []interface{}s append(s, 1, 2, 3, 字符串, 1.1, true)fmt.Println(s) }map和切片也可以使用空接口 func main() {m : make(map[string]interface{})m[打野] 韩信fmt.Println(m)i : []interface{}{1, 2, 3, 4.5, 哈哈, true}fmt.Println(i) }类型断言 通过类型断言可以判断空接口中存储的数据类型 定义格式 value,ok:m.(T)value表示变量m的值ok表示布尔类型变量如果断言成功为true否则为falsem表示空接口类型的变量如果推断成功就把数据赋值给valueT表示断言的类型 演示 func main() {var i interface{}i 123// 如果i是int类型就会把结果赋值给ok把数据123赋值给valuevalue, ok : i.(int)// 如果是123就会打印断言成功不是123就会打印断言失败if ok {fmt.Println(断言成功, value)} else {fmt.Println(断言失败)} }计算器案例 func main() {var c ObjectInitializationresult : c.Create(, 1, 2)fmt.Println(result) }// 接口 type Calculate interface {GetResult() int }// 公共参数类 type Num struct {num1, num2 int }// 加法类 type Add struct {Num }func (add *Add) GetResult() int {return add.num1 add.num2 }// 减法类 type Sub struct {Num }func (sub *Sub) GetResult() int {return sub.num1 - sub.num2 }// 乘法类 type Multiplication struct {Num }func (multiplication *Multiplication) GetResult() int {return multiplication.num1 * multiplication.num2 }// 除法类 type DivisionMethod struct {Num }func (divisionMethod *DivisionMethod) GetResult() int {return divisionMethod.num1 / divisionMethod.num2 }// 用于完成选择哪个方法的调用 type ObjectInitialization struct{}func InvokingFunction(c Calculate) int {return c.GetResult() } func (o *ObjectInitialization) Create(oi string, num1, num2 int) int {switch oi {case :add : Add{Num{num1, num2}}return InvokingFunction(add)case -:sub : Sub{Num{num1, num2}}return InvokingFunction(sub)default:return 0} }
http://www.zqtcl.cn/news/473712/

相关文章:

  • 电子商务网站模板 html专业网站建设服务报价
  • 网页设计和网站建设的区别研发一款app要多少钱
  • seo网站建设方案建个企业网站需要多少钱
  • 搭建网站的软件网页动态设计
  • 好的界面建筑网站甘孜网站建设
  • 电子商务网站创建过程网站排名提升软件
  • 青岛企业如何建网站购买网站建站
  • 广东自考网站建设管理网站做ddns解析
  • 网站建设分类如何重启网站服务器
  • 新蒲建设集团网站怎么把源码做网站
  • 嘉兴建设局网站在线制作头像框
  • 苏州行业网站建设服务网页制作需要学什么技术
  • 二 网站建设的重要性东莞seo建站优化收费
  • 农业公司注册流程及费用快排seo排名软件
  • 响应式中文网站欣赏机wordpress
  • 如何建网站并做推广亚马逊网站怎么做推广
  • 做好网站建设总结免费开发app平台下载
  • 哈尔滨建站免费模板app网站开发要多少钱
  • 大连网站设计九首选仟亿科技怎么做百度网站会显示图片在旁边
  • 南京营销网站建设wordpress图片购买下载
  • 装修平台网站制作word模板
  • 网站建设捌金手指花总十软文写作技巧
  • 做网站优化有用吗网站开发包括什么软件
  • 在线音乐网站开发现状有什么网站接效果图做的
  • 网站开发自学难吗上海网站建设百度推广公司哪家好
  • 建设部网站官网四库一平台房地产网站大全
  • 做外贸如何建立网站微信信息流广告投放
  • 上海工程建设招投标网站开发购物网站描述
  • 网站系统维护一般多久电商关键字优化
  • 孝感市建设局网站宁波seo网络推广价格