接口定义

      Interface类型可以定义一组方法,但是这些不需要实现。并且interface不能 包含任何变量。

type Interface interface {
  test1(a, b int) bool
  test2()
}

interface类型默认是一个指针

 空接口(a interface{})可以被任何类型所实现,如动物可以被猫、狗、人等实现,反之人不一定能实现动物

func main() {
  var a interface{}

  var b int
  a = b

  fmt.Printf("%T\n", a)

  var s string
  a = s

  fmt.Printf("%T", a)
}

golang interface

接口只是一种规范,并没有实现,不能直接调用

type testInterface int

func (t testInterface) test1(a, b int) bool {
	return a < b
}

func (t testInterface) test2() {
	fmt.Println("test2")
}

func main() {
	var a Interface
	a.test2()
}

golang interface

接口实现

Golang中的接口,不需要显示的实现。只要一个变量,含有接口类型中的所有方法,那么这个变量就实现这个接口。因此,golang中没有implement 类似的关键字。

type testInterface int

func (t testInterface) test1(a, b int) bool {
  return a < b
}

func (t testInterface) test2() {
  fmt.Println("test2")
}

func main() {
  var a testInterface
  fmt.Printf("%T\n", a.test1(1, 2))
  a.test2()
}

golang interface

接口实现时需要实现规范内的所有方法,否则会报错

type Interface interface {
	test1(a, b int) bool
	test2()
}

type testInterface int

func (t testInterface) test1(a, b int) bool {
	return a < b
}

func main() {
	var test testInterface
	var i Interface

	i = test

	fmt.Println(test)

}

golang interface

cannot use test (type testInterface) as type Interface in assignment:
testInterface does not implement Interface (missing test2 method)

如果一个变量含有了多个interface类型的方法,那么这个变量就实现了多个 接口。

type human interface { //人类
	get_country() //国家
}

type lang interface {
	speek() //语言
}

type chinesePeople struct { //中国人
	country string
}

func (c chinesePeople) get_country() {
	fmt.Println(c.country)
}

type japanese struct { //日本人
	country string
}

func (c japanese) get_country() {
	fmt.Println(c.country)
}

func (c japanese) speek() { //说的语言
	fmt.Println("ハロー")
}

func main() {

	var c = chinesePeople{
		country: "china",
	}

	var j = japanese{
		country: "japan",
	}
	var h human
	var langage lang

	h = c

	h.get_country()

	langage = j
	h = j
	h.get_country()
	langage.speek()
}

japanese结构体实现了两个接口

golang interface

chinese结构体实现了country接口如果使用lang赋值会提示未实现该接口

cannot use c (type chinesePeople) as type lang in assignment:
chinesePeople does not implement lang (missing speek method)

接口嵌套

一个接口可以嵌套在另外的接口

package main

type human interface {
	run()
	eyes()
}

type info interface {
	country()
	sex()
}

type chinesePeople interface {
	human
	info
}

func main() {

}

类型断言

接口是一般类型,不知道具体类型,如果要转成具体类型

语法

目标类型 := x.(t)
目标类型,布尔值 := x.(t)

两者区别为,安全类型与不安全类断言,非安全类型如果判定失败,这个操作会抛出panic

如:

var i interface{}
i = 10
j := i.(string)
fmt.Printf("%T\n", j)

golang interface

断言的操作对象x是一个nil接口值,那么不论被断言的类型T是什么这个类型断言都会失败。

var i interface{}
i = nil
j := i.(nil)

 

安全类型的断言

t, ok := i.(int)
if ok == false {
  fmt.Printf("失败,断言的类型为%T,而i的类型为%T", t, i)
  os.Exit(1)
}

fmt.Printf("成功%T", t)

golang interface

 

  

相关文章:

  • 2021-07-28
  • 2022-12-23
  • 2022-12-23
  • 2021-08-12
  • 2022-03-06
  • 2021-07-10
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-08
  • 2021-10-31
  • 2021-09-10
  • 2021-08-27
相关资源
相似解决方案