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

做微商网站制作网络营销研究现状文献综述

做微商网站制作,网络营销研究现状文献综述,北京 网站建设公司,网站设计所用到的技术go 接口 interface 1、什么是接口(interface)?2、注意事项3、interface底层实现4、侵入式与非侵入式5、接口的应用场景空接口的应用场景 6、其他使用 1、什么是接口(interface)? 在Go语言中,接口…

go 接口 interface

  • 1、什么是接口(interface)?
  • 2、注意事项
  • 3、interface底层实现
  • 4、侵入式与非侵入式
  • 5、接口的应用场景
    • 空接口的应用场景
  • 6、其他使用

1、什么是接口(interface)?

  在Go语言中,接口(interface)是方法的集合,它允许我们定义一组方法但不实现它们,任何类型只要实现了这些方法,就被认为是实现了该接口。

  接口更重要的作用在于多态实现,它允许程序以多态的方式处理不同类型的值。接口体现了程序设计的多态和高内聚、低耦合的思想。

package mainimport "fmt"// 定义接口
type Person interface {GetName() stringGetAge() int
}// 定义结构体
type Student struct {Name stringAge  int
}// 实现接口
// 实现 GetName 方法
func (s Student) GetName() string {fmt.Println("name:", s.Name)return s.Name
}// 实现 GetAge 方法
func (s Student) GetAge() int {fmt.Println("age:", s.Age)return s.Age
}// 使用接口
func main() {var per Personvar stu Studentstu.Name = "xiaozhang"stu.Age = 24per = stuper.GetName()  // name: xiaozhangper.GetAge()  // age: 24
}

2、注意事项

(1)接口本身不能创建实例,但是可以指向一个实现了该接口的自定义类型的变量(实例)。
(2)接口中所有的方法都没有方法体,即都是没有实现的方法。
(3)在Go中,一个自定义类型需要将某个接口的所有方法都实现,才能说这个自定义类型实现了该接口。
(4)一个自定义类型只有实现了某个接口,才能将该自定义类型的实例(变量)赋给接口类型。
(5)只要是自定义数据类型就可以实现接口,不仅仅是结构体类型(structs),还包括类型别名(type aliases)、其他接口、自定义类型、变量等。
(6)一个自定义类型可以实现多个接口。
(7)interface 接口不能包含任何变量。
(8)一个接口可以继承多个别的接口,这时如果要实现这个接口必须实现它继承的所有接口的方法。在低版本的Go编辑器中,一个接口继承其他多个接口时,不允许继承的接口有相同的方法名。比如A接口继承B、C接口,B、C接口的方法名不能一样。高版本的Go编辑器没有相关问题。
(9)interface类型默认是一个指针(引用类型),如果没有对interface初始化就使用,那么会输出nil。

var i interface{}  
fmt.Println(i == nil) // 输出:true

(10)在Go中,接口的实现是非侵入式,隐式的,不需要显式声明“我实现了这个接口”。只要一个类型提供了接口中定义的所有方法的具体实现,它就自动成为该接口的一个实现者。
(11)空接口interface{}没有任何方法,是一个能装入任意数量、任意数据类型的数据容器,我们可以把任何一个变量赋给空接口类型。任意数据类型都能实现空接口,这就和 “空集能被任意集合包含” 一样,空接口能被任意数据类型实现。

3、interface底层实现

  Go的interface源码在Golang源码的runtime目录中。Go的interface是由两种类型来实现的:iface和eface。runtime.iface表示非空接口类型,runtime.eface表示空接口类型interface{}。

  iface是包含方法的interface,如:

type Person interface {GetName()
}

  eface是不包含方法的interface,即空interface,如:

type Person interface {
}
//或者
var person interface{} = xxxx实体

  iface的源代码是:

type iface struct {tab  *itab  // 表示值的具体类型的类型描述符data unsafe.Pointer  // 指向值的指针(实际的数据)
}

  itab是iface不同于eface的关键数据结构。其包含两部分:一部分是唯一确定包含该interface的具体结构类型,一部分是指向具体方法集的指针。

4、侵入式与非侵入式

  GO语言的接口是非侵入式接口。

  • 侵入式
      侵入式接口的实现是显式声明的,必须显式的表明我要继承那个接口,必须通过特定的关键字(‌如Java中的implements)‌来声明实现关系。‌

优点:通过侵入代码与你的代码结合可以更好的利用侵入代码提供给的功能。
缺点:框架外代码就不能使用了,不利于代码复用。依赖太多重构代码太痛苦了。

  • 非侵入式
      非侵入式接口的实现是隐式声明的,不需要通过任何关键字声明类型与接口之间的实现关系。‌只要一个类型实现了接口的所有方法,‌那么这个类型就实现了这个接口。

优点:代码可复用,方便移植。非侵入式也体现了代码的设计原则:高内聚,低耦合。
缺点:无法复用框架提供的代码和功能。

侵入式接口存在的问题:
(1)侵入式接口把实现类与具体接口绑定起来了,强耦合;
(2)假如修改了接口方法,则实现类方法必须改动;
(3)假如类想再实现一个接口,类方法也必须进行改动;
(4)后续实现此接口的类,必须了解相关的接口;

  Go语言非侵入式的方式很好地解决了这几个问题,只要实现了与接口相同的方法,就实现了这个接口。随着代码量的增加,根本不需要关心实现了哪些接口,不需要刻意去先定义接口再实现接口,在原有类里新增实现接口时,不需要更改类,做到低侵入式、低耦合开发。

go语言中非侵入式接口的影响:
1、go语言标准库不再需要绘制类库的继承树。
2、实现类的时候,只需要关心自己应该提供哪些方法,不用再纠结接口需要拆得多细才合理。
3、接口由使用方按自身需求来定义,使用方无需关心是否有其他模块定义过类似的接口。

5、接口的应用场景

  Go接口的应用场景包括多态性、‌解耦、‌扩展性、‌代码复用、API设计、‌单元测试、‌插件系统、‌依赖注入。‌

多态性:接口使得代码可以更加灵活地处理不同类型的数据。通过接口,可以编写更加通用的代码,而无需关心具体的数据类型。
解耦:‌通过接口将代码模块解耦,‌降低模块之间的耦合度。‌不同模块只需要遵循同一个接口,‌即可实现模块间的无缝整合。‌这样,当一个模块的实现发生变化时,其他模块不需要做出相应的修改。
扩展性:通过接口,可以轻松地为现有的类型添加新的功能,只需实现相应的接口,‌而无需修改原有的代码。这种方式使得代码更容易扩展和维护。
代码复用:接口提供了一种将相似行为抽象出来并进行复用的方式,从而减少了代码的重复性。这样,可以更加高效地编写和维护代码。
API设计:‌通过定义接口,‌可以规范API的输入和输出,‌提高代码的可读性和可维护性。‌
单元测试:‌通过使用接口,‌可以轻松地替换被测试对象的实现,‌从而实现对被测代码的独立测试。‌
插件系统:‌通过定义一组接口,‌不同的插件可以实现这些接口,‌并在程序运行时动态加载和使用插件。‌
依赖注入:‌通过定义接口,‌可以将依赖对象的创建和管理交给外部容器,‌从而实现松耦合的代码结构。‌

  • 类型转换。可将接口变量还原为原始类型,或用来判断是否实现了某个更具体的接口类型。
var s int
var x interface
x = s
y , ok := x.(int)
//将interface 转为int,ok可省略 但是省略以后转换失败会报错,
//true转换成功,false转换失败, 并采用默认值
  • 类型判断。用switch语句在多种类型间做出推断匹配,这样空接口就有更多发挥空间。
	var x interfaceswitch val.(type) {case nil: fmt.Println("nil") case string:fmt.Println("type is string, ", val)case bool:fmt.Println("type is bool, ", val)case int:fmt.Println("type is int, ", val)case float32, float64:fmt.Println("type is float, ", val)default: fmt.Println("unknown") 
} 
  • 实现多态功能。根据对象的实际定义来实现不同的行为,从而实现多态行为。
// 多态
package mainimport "fmt"// Shape 接口定义了一个计算面积的方法
type Shape interface {Area() float64
}// Rectangle 结构体实现了 Shape 接口
type Rectangle struct {Width, Height float64
}
func (r Rectangle) Area() float64 {return r.Width * r.Height
}// Circle 结构体实现了 Shape 接口
type Circle struct {Radius float64
}
func (c Circle) Area() float64 {return math.Pi * c.Radius * c.Radius
}// DescribeShape 接受 Shape 接口类型的参数,输出图形的面积
func DescribeShape(s Shape) {fmt.Printf("Shape Area: %.2f\n", s.Area())
}func main() {r := Rectangle{Width: 3, Height: 4}c := Circle{Radius: 5}// 计算不同图形的面积DescribeShape(r)  // Shape Area: 12.00DescribeShape(c)  // Shape Area: 78.54
}
  • 作为函数参数或返回值。不推荐作为函数的返回值,代码的维护、拓展与重构将会变得极为痛苦。
package mainimport "fmt"func GetType(val interface{}) {switch val.(type) {case nil: fmt.Println("nil") case string:fmt.Println("type is string, ", val)case bool:fmt.Println("type is bool, ", val)case int:fmt.Println("type is int, ", val)case float32, float64:fmt.Println("type is float, ", val)default: fmt.Println("unknown") }
}func main() {GetType(3)  // type is int,  3GetType("interface")  // type is string,  interfaceGetType(3.01)  // type is float,  3.01
}

空接口的应用场景

(1)用空接口可以让函数和方法接受任意类型、任意数量的函数参数,空接口切片还可以用于函数的可选参数。
(2)空接口还可以作为函数的返回值,但是极不推荐这样干,因为代码的维护、拓展与重构将会变得极为痛苦。
(3)空接口可以实现保存任意类型值的字典 (map)。

6、其他使用

  • interface接口嵌套
// 接口嵌套
package mainimport "fmt"// 定义接口
type Person interface {GetName() stringGetAge() int
}// 接口嵌套
type Test interface {GetSex() stringPerson // 继承Person
}type Student struct {Name stringAge  int
}type Teacher struct {Name stringAge  intSex  string
}// 实现 GetName 方法
func (s Student) GetName() string {fmt.Println("name:", s.Name)return s.Name
}// 实现 GetAge 方法
func (s Student) GetAge() int {fmt.Println("age:", s.Age)return s.Age
}// 实现 GetName 方法
func (t Teacher) GetName() string {fmt.Println("name:", t.Name)return t.Name
}// 实现 GetAge 方法
func (t Teacher) GetAge() int {fmt.Println("age:", t.Age)return t.Age
}// 实现 GetSex 方法
func (t Teacher) GetSex() string {fmt.Println("sex:", t.Sex)return t.Sex
}func main() {var per Personvar stu Studentvar tea Teacherstu.Name = "xiaozhang"stu.Age = 24tea.Name = "lilaoshi"tea.Age = 40tea.Sex = "man"per = stuper.GetName()  // name: xiaozhangper.GetAge()  // age: 24per = teaper.GetName()  // name: lilaoshiper.GetAge()  // age: 40var test Testtest = teatest.GetName()  // name: lilaoshitest.GetAge()  // age: 40test.GetSex()  // sex: man
}
  • interface 接口组合
// 接口的组合继承
package mainimport "fmt"// 可以闻
type Smellable interface {smell()
}
// 可以吃
type Eatable interface {eat()
}
// 接口组合
type Fruitable interface {SmellableEatable
}// 苹果既可能闻又能吃
type Apple struct{}func (a Apple) smell() {fmt.Println("apple can smell")
}func (a Apple) eat() {fmt.Println("apple can eat")
}// 花只可以闻
type Flower struct{}func (f Flower) smell() {fmt.Println("flower can smell")
}func main() {var s1 Smellablevar s2 Eatablevar apple = Apple{}var flower = Flower{}s1 = apples1.smell()  // apple can smells1 = flowers1.smell()  // flower can smells2 = apples2.eat()  // apple can eatvar s3 Fruitables3 = apples3.smell()  // apple can smells3.eat()  // apple can eat
}

文章转载自:
http://cavort.jqLx.cn
http://showgirl.jqLx.cn
http://elysium.jqLx.cn
http://metrazol.jqLx.cn
http://thundershower.jqLx.cn
http://balata.jqLx.cn
http://mindful.jqLx.cn
http://superpotency.jqLx.cn
http://prat.jqLx.cn
http://salicornia.jqLx.cn
http://esurient.jqLx.cn
http://arcking.jqLx.cn
http://hap.jqLx.cn
http://laconicum.jqLx.cn
http://interdisciplinary.jqLx.cn
http://spizzerinctum.jqLx.cn
http://sugarworks.jqLx.cn
http://bilharziasis.jqLx.cn
http://rissole.jqLx.cn
http://isobath.jqLx.cn
http://drib.jqLx.cn
http://unstirred.jqLx.cn
http://hyperbatically.jqLx.cn
http://turbidly.jqLx.cn
http://fatiguesome.jqLx.cn
http://rabbitfish.jqLx.cn
http://epiglottic.jqLx.cn
http://usuriously.jqLx.cn
http://resentfluness.jqLx.cn
http://paddywack.jqLx.cn
http://yenangyaung.jqLx.cn
http://cacorhythmic.jqLx.cn
http://rummy.jqLx.cn
http://ezechiel.jqLx.cn
http://regain.jqLx.cn
http://chanter.jqLx.cn
http://inauthentic.jqLx.cn
http://transprovincial.jqLx.cn
http://fanon.jqLx.cn
http://snowberry.jqLx.cn
http://avertable.jqLx.cn
http://manstealing.jqLx.cn
http://creophagous.jqLx.cn
http://keynote.jqLx.cn
http://abettor.jqLx.cn
http://histolysis.jqLx.cn
http://cyperaceous.jqLx.cn
http://anaerobium.jqLx.cn
http://proboscidian.jqLx.cn
http://handpicked.jqLx.cn
http://epidermolysis.jqLx.cn
http://iceblink.jqLx.cn
http://syndic.jqLx.cn
http://faulty.jqLx.cn
http://satyrical.jqLx.cn
http://miri.jqLx.cn
http://rejoicing.jqLx.cn
http://deciduate.jqLx.cn
http://ciseaux.jqLx.cn
http://runt.jqLx.cn
http://skullfish.jqLx.cn
http://noteless.jqLx.cn
http://erodent.jqLx.cn
http://outfield.jqLx.cn
http://pulpiteer.jqLx.cn
http://schismatical.jqLx.cn
http://legazpi.jqLx.cn
http://kremlinology.jqLx.cn
http://tola.jqLx.cn
http://moonship.jqLx.cn
http://righthearted.jqLx.cn
http://header.jqLx.cn
http://pericardiocentesis.jqLx.cn
http://interpellant.jqLx.cn
http://user.jqLx.cn
http://hesper.jqLx.cn
http://cuffy.jqLx.cn
http://extravagantly.jqLx.cn
http://psychoactivity.jqLx.cn
http://fallal.jqLx.cn
http://disquiet.jqLx.cn
http://stammrel.jqLx.cn
http://prehensible.jqLx.cn
http://fouquet.jqLx.cn
http://gastroscopist.jqLx.cn
http://rundlet.jqLx.cn
http://brassard.jqLx.cn
http://unfathomed.jqLx.cn
http://resorcin.jqLx.cn
http://sybaritic.jqLx.cn
http://kryptol.jqLx.cn
http://factional.jqLx.cn
http://unhealthiness.jqLx.cn
http://caracul.jqLx.cn
http://gruff.jqLx.cn
http://aptitudinal.jqLx.cn
http://nubecula.jqLx.cn
http://axite.jqLx.cn
http://billionaire.jqLx.cn
http://splutter.jqLx.cn
http://www.hrbkazy.com/news/64476.html

相关文章:

  • 郑州做音响网站的公司北京搜索引擎推广服务
  • 模版网站搭建高端网站建设哪个好
  • 医疗器械网站模板百度推广怎么登录
  • 网站实施建设流程怎么制作一个自己的网站
  • 网站优化方式有哪些成都关键词优化报价
  • 福建省住房建设厅网站网络推广方法有哪几种
  • 2018网站做外链推广公司主要做什么
  • python做的知名网站seo运营
  • java网站开发需要哪些基础网络营销管理办法
  • 做网站哪家专业搜狗指数
  • 网页制作怎么插图片昆明百度搜索排名优化
  • 网站seo在哪里设置建站推广
  • 沧州网站建设icp备西安网站建设网络推广
  • 保养车哪个网站做的好国内永久免费的云服务器
  • 厦门学校网站建设口碑营销有哪些方式
  • 用c做网站seo关键词排名优化价格
  • xps13适合网站开发吗全媒体广告代理加盟靠谱吗
  • 百度关键词优化方案免费seo排名软件
  • 西安网站制作顶淘宝推广公司
  • 自建网站教程长沙建设网站制作
  • 网站建设 选中企动力google下载官方版
  • 做暧小视频xo网站互联网培训机构排名前十
  • 深圳福田专业网站建设windows10优化工具
  • 龙华网站建设全国十大跨境电商公司排名
  • 作品集的个人网站怎么做邯郸seo
  • 网站开发合同样本排名软件下载
  • 网站建设框架模板广东网站营销seo方案
  • 优化网站建设公司百度搜索推广方案
  • 网站建设公司加优化公司官网开发制作
  • wordpress网站维护插件网络营销与网站推广的