Go 接口(一)

目录:

  1. 接口声明
  2. 接口使用
  3. 空接口
  4. 类型断言
  5. 类型可以和接口相互比较
接口声明

在面向对象的领域里,接口一般是这样定义的: 接口定义一个对象的行为。接口值指定了对象应该做什么,至于如何实现这个行为,则由对象本身去确定。


接口是方法签名的集合


接口指定了一个类应该具有的方法,并由改类型决定如何实现这些方法

定义一个接口

package main

import "fmt"

// 定义一个借口
type Duck interface {
	// 只需写方法名、参数、如果方法有返回值,需指定返回值类型
	run()
	speak()
}

type TDuck struct {
	name string
	age  int
}
// 绑定方法
func (t TDuck) run() {
	fmt.Println("t_run")
}
func (t TDuck) speak() {
	fmt.Println("t_speak")
}

func main() {

}

回到目录



接口使用
package main

import "fmt"

// 定义一个借口
type Duck interface {
	// 只需写方法名、参数、如果方法有返回值,需指定返回值类型
	run()
	speak()
}

type TDuck struct {
	name string
	age  int
}
type PDuck struct {
	name string
	age  int
	sex  int
}
func (t TDuck) run() {
	fmt.Println("t_run")
}
func (t TDuck) speak() {
	fmt.Println("t_speak")
}
func (p PDuck) run() {
	fmt.Println("p_run")
}
func (p PDuck) speak() {
	fmt.Println("p_speak")
}

func main() {
	t := TDuck{}
	p := PDuck{}

	run(t)
	run(p)

}

func run(d Duck) {
	// 这里体现了面向对象的多态性,同样是duck,会有不同的run方法
	d.run()
}

回到目录



空接口

没有包含方法的接口称为空接口。空接口表示为 interface{} 。由于空接口没有方法,因此所有类型都实现了空接口


可以接受任意类型的参数

示例:

package main

import "fmt"

func main() {
	a := 55
	describe(a)
	b := "jeff"
	describe(b)
	c := struct {
		string
	}{"shanghai"}
	describe(c)
}

func describe(i interface{}) {
	fmt.Printf("Type = %T, Value = %v\n", i, i)
}

输出:

Type = int, Value = 55
Type = string, Value = jeff
Type = struct { string }, Value = {shanghai}

回到目录



类型断言
type Duck interface {
	// 只需写方法名、参数、如果方法有返回值,需指定返回值类型
	run()
	speak()
}

type TDuck struct {
	name string
	age  int
}

func (t TDuck) run() {
	fmt.Println("t_run")
}
func (t TDuck) speak() {
	fmt.Println("t_speak")
}
func (t TDuck) other() {
	fmt.Println("t_other")
}


func main() {
	t := TDuck{}

	run(t)

}

func run(d Duck) {
	// 断定你是TDuck,此时接口中没有定义的other方法可以使用
	a:=d.(TDuck)
	a.other()
}

类型选择

// 定义一个借口
type Duck interface {
	// 只需写方法名、参数、如果方法有返回值,需指定返回值类型
	run()
	speak()
}

type TDuck struct {
	name string
	age  int
}
type PDuck struct {
	name string
	age  int
	sex  int
}
func (t TDuck) run() {
	fmt.Println("t_run")
}
func (t TDuck) speak() {
	fmt.Println("t_speak")
}
func (t TDuck) other() {
	fmt.Println("t_other")
}
func (p PDuck) run() {
	fmt.Println("p_run")
}
func (p PDuck) speak() {
	fmt.Println("p_speak")
}
func (t PDuck) other() {
	fmt.Println("p_other")
}

func main() {
	p := PDuck{}
	t:= TDuck{}
	run(p)
	run(t)

}

func run(d Duck) {
	switch a:=d.(type){
	case TDuck:
		fmt.Println("TDuck")
		a.other()
	case PDuck:
		fmt.Println("PDuck")
		a.other()
	default:
		fmt.Println("unknown type")
	}
}

回到目录



类型可以和接口相互比较
package main

import "fmt"
//定义了一个接口,里面有Describe方法
type Describer interface {
	Describe()
}
//定义了一个结构体,有两个属性
type Person struct {
	name string
	age  int
}
//给结构体绑定方法,实现了Describer接口
func (p Person) Describe() {
	fmt.Printf("%s is %d years old", p.name, p.age)
}

func findType(i interface{}) {
	switch v := i.(type) {
	case Describer:
		v.Describe()
	default:
		fmt.Printf("unknown type\n")
	}
}

func main() {
	//findType("Naveen")
	//p := Person{
	//	name: "Naveen R",
	//	age:  25,
	//}
	//findType(p)
	findType(12)
	b:=[2]int{2,3,}
	findType(b)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值