Golang interface

2020-10-26  本文已影响0人  aneirin

接口在Golang中表示一种抽象的数据类型,它用来定义对象的行为,具体的实现由对象决定。程序编写时如果仅仅定义接口是没有实际意义的,我们需要自定义类型,如果类型对某个接口的所有方法提供了定义,就说这个类型实现了该接口。

type People interface {
    GetName() string                // 带有返回值的方法
    GetAge(id int)                     // 带有参数的方法
    GetTelephone(a int) int      //同时带有参数和返回值的方法
}

一个接口是方法集的蓝图,在使用前必须有类型实现它,如果不能满足这个条件,接口是不能使用的。

package main

import "fmt"

type People interface {
    GetName(name string)
    GetAge(a int) int
}

type Ple struct {
}

func (p Ple) GetName(name string) {
    fmt.Println("People Name:\t", name)
}

func (p Ple) GetAge(a int) int {
    return a
}

func main() {
    var p1 People
    p1 = Ple{}
    p1.GetName("John Doe")
    fmt.Println("Ple Age:", p1.GetAge(25))
}

上面代码中Ple类型实现了接口People声明的所有方法,此时,就可以将Ple的实例赋值给People的变量p1。

package main

import "fmt"

type People interface {
    GetName()
}

type Animal interface {
    GetAge()
}

type Custom struct 
}

func (c Custom) GetName() {
    fmt.Println("name is xiaoli")
}

func (c Custom) GetAge() {
    fmt.Println("age is 5")
}

func main() {
    var p People = Custom{}
    p.GetName()
    p.GetAge()
    var o Custom = p.(Custom)
    o.GetAge()
}

将“Custom”类型的实例赋值给接口变量p,可以直接通过接口变量p调用方法“GetName”,但是不能直接调用方法“GetAge”,因为接口“People”没有方法“GetAge”,这时使用类型断言得到实际的自定义类型值,就可以调用接口“People”本不具有的方法“GetAge”。(上面代码的最后两行可以直接写成“p.(Custom).GetAge()”)

package main
 
 import "fmt"
 
 type Student struct {
     name,address string
 }
 
 func (s *Student) Init (n,d string) {
     s.name = n
     s.address = d
 }
 
 func (s *Student) Print () {
     fmt.Printf("Student name is %s,address is %s \n",s.name,s.address)
 }
 
 type Printer interface {
     Print()
 }
 
 func main() {
     var s Student
     s.Init("xiaoli","shenzhen")
     var p Printer
     p = &s  //赋值地址类型
     p.Print()
 }

因为Printer方法有指针接收者:“func (s *Student) Print ()”,所以给接口变量赋值时,必须使用变量地址:“p = &s”,如果不符合,会有如下报错:

# command-line-arguments
./interface-test.go:26:4: cannot use s (type Student) as type Printer in assignment:
    Student does not implement Printer (Print method has pointer receiver)
上一篇 下一篇

猜你喜欢

热点阅读