Warning: Undefined array key "custom_message" in /www/wwwroot/bbs.aaronyang.cc/wp-content/plugins/wpcopyrights/index.php on line 105

接口(一)

接口

上一节:第十七篇 方法
下一节:第十九篇 接口二

欢迎来到第 18 个教程。
接口共有两个教程,这是我们接口的第一个教程。

什么是接口?

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

在 Go 语言中,接口就是方法签名(Method Signature)的集合。当一个类型定义了接口中的所有方法,我们称它实现了该接口。这与面向对象编程(OOP)的说法很类似。接口指定了一个类型应该具有的方法,并由该类型决定如何实现这些方法

例如,WashingMachine 是一个含有 Cleaning()Drying() 两个方法的接口。任何定义了 Cleaning()Drying() 的类型,都称它实现了 WashingMachine 接口。

接口的声明与实现

让我们编写代码,创建一个接口并且实现它。

package main

import (  
    "fmt"
)

//interface definition
type VowelsFinder interface {  
    FindVowels() []rune
}

type MyString string

//MyString implements VowelsFinder
func (ms MyString) FindVowels() []rune {  
    var vowels []rune
    for _, rune := range ms {
        if rune == 'a' || rune == 'e' || rune == 'i' || rune == 'o' || rune == 'u' {
            vowels = append(vowels, rune)
        }
    }
    return vowels
}

func main() {  
    name := MyString("Sam Anderson")
    var v VowelsFinder
    v = name // possible since MyString implements VowelsFinder
    fmt.Printf("Vowels are %c", v.FindVowels())

}

在上面程序的第 8 行,创建了一个名为 VowelsFinder 的接口,该接口有一个 FindVowels() []rune 的方法。

在接下来的一行,我们创建了一个 MyString 类型。

在第 15 行,我们给接受者类型(Receiver Type) MyString 添加了方法 FindVowels() []rune。现在,我们称 MyString 实现了 VowelsFinder 接口。这就和其他语言(如 Java)很不同,其他一些语言要求一个类使用 implement 关键字,来显式地声明该类实现了接口。而在 Go 中,并不需要这样。如果一个类型包含了接口中声明的所有方法,那么它就隐式地实现了 Go 接口

在第 28 行,v 的类型为 VowelsFindername 的类型为 MyString,我们把 name 赋值给了 v。由于 MyString 实现了 VowelFinder,因此这是合法的。在下一行,v.FindVowels() 调用了 MyString 类型的 FindVowels 方法,打印字符串 Sam Anderson 里所有的元音。该程序输出 Vowels are [a e o]

祝贺!你已经创建并实现了你的第一个接口。

接口的实际用途

前面的例子教我们创建并实现了接口,但还没有告诉我们接口的实际用途。在上面的程序里,如果我们使用 name.FindVowels(),而不是 v.FindVowels(),程序依然能够照常运行,但接口并没有体现出实际价值。

因此,我们现在讨论一下接口的实际应用场景。

我们编写一个简单程序,根据公司员工的个人薪资,计算公司的总支出。为了简单起见,我们假定支出的单位都是美元。

package main

import (  
    "fmt"
)

//接口
type SalaryCalculator interface {  
    CalculateSalary() int
}

//结构体1
type Permanent struct {  
    empId    int
    basicpay int
    pf       int
}

//结构体2
type Contract struct {  
    empId  int
    basicpay int
}

//方法1
//salary of permanent employee is sum of basic pay and pf
func (p Permanent) CalculateSalary() int {  
    return p.basicpay + p.pf
}

//方法2
//salary of contract employee is the basic pay alone
func (c Contract) CalculateSalary() int {  
    return c.basicpay
}

/*
total expense is calculated by iterating though the SalaryCalculator slice and summing  
the salaries of the individual employees  
*/
func totalExpense(s []SalaryCalculator) {  
    expense := 0
    for _, v := range s {
        expense = expense + v.CalculateSalary()
    }
    fmt.Printf("Total Expense Per Month $%d", expense)
}

func main() {  
    pemp1 := Permanent{1, 5000, 20}
    pemp2 := Permanent{2, 6000, 30}
    cemp1 := Contract{3, 3000}
    employees := []SalaryCalculator{pemp1, pemp2, cemp1}
    totalExpense(employees)

}

上面程序的第 7 行声明了一个 SalaryCalculator 接口类型,它只有一个方法 CalculateSalary() int

在公司里,我们有两类员工,即第 12 行和第 19 行定义的结构体:PermanentContract。长期员工(Permanent)的薪资是 basicpaypf 相加之和,而合同员工(Contract)只有基本工资 basicpay。在第 27 行和第 33 行中,方法 CalculateSalary 分别实现了以上关系。由于 PermanentContract 都声明了该方法,因此它们都实现了 SalaryCalculator 接口。

第 41 行声明的 totalExpense 方法体现出了接口的妙用。该方法接收一个 SalaryCalculator 接口的切片([]SalaryCalculator)作为参数。在第 53 行,我们向 totalExpense 方法传递了一个包含 PermanentContact 类型的切片。在第 44 行中,通过调用不同类型对应的 CalculateSalary 方法,totalExpense 可以计算得到支出。

这样做最大的优点是:totalExpense 可以扩展新的员工类型,而不需要修改任何代码。假如公司增加了一种新的员工类型 Freelancer,它有着不同的薪资结构。Freelancer只需传递到 totalExpense 的切片参数中,无需 totalExpense 方法本身进行修改。只要 Freelancer 也实现了 SalaryCalculator 接口,totalExpense 就能够实现其功能。

该程序输出 Total Expense Per Month $14050

接口的内部表示

我们可以把接口看作内部的一个元组 (type, value)type 是接口底层的具体类型(Concrete Type),而 value 是具体类型的值。

我们编写一个程序来更好地理解它。

package main

import (  
    "fmt"
)

type Test interface {  
    Tester()
}

type MyFloat float64

func (m MyFloat) Tester() {  
    fmt.Println(m)
}

func describe(t Test) {  
    fmt.Printf("Interface type %T value %v\n", t, t)
}

func main() {  
    var t Test
    f := MyFloat(89.7)
    t = f
    describe(t)
    t.Tester()
}

Test 接口只有一个方法 Tester(),而 MyFloat 类型实现了该接口。在第 24 行,我们把变量 fMyFloat 类型)赋值给了 tTest 类型)。现在 t 的具体类型为 MyFloat,而 t 的值为 89.7。第 17 行的 describe 函数打印出了接口的具体类型和值。该程序输出:

Interface type main.MyFloat value 89.7  
89.7

空接口

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

package main

import (  
    "fmt"
)

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

func main() {  
    s := "Hello World"
    describe(s)
    i := 55
    describe(i)
    strt := struct {
        name string
    }{
        name: "Naveen R",
    }
    describe(strt)
}

在线运行程序

在上面的程序的第 7 行,describe(i interface{}) 函数接收空接口作为参数,因此,可以给这个函数传递任何类型。

在第 13 行、第 15 行和第 21 行,我们分别给 describe 函数传递了 stringintstruct。该程序打印:

Type = string, value = Hello World  
Type = int, value = 55  
Type = struct { name string }, value = {Naveen R}

类型断言

类型断言用于提取接口的底层值(Underlying Value)。

在语法 i.(T) 中,接口 i 的具体类型是 T,该语法用于获得接口的底层值。

一段代码胜过千言。下面编写个关于类型断言的程序。

package main

import (  
    "fmt"
)

func assert(i interface{}) {  
    s := i.(int) //get the underlying int value from i
    fmt.Println(s)
}
func main() {  
    var s interface{} = 56
    assert(s)
}

在第 12 行,s 的具体类型是 int。在第 8 行,我们使用了语法 i.(int) 来提取 i 的底层 int 值。该程序会打印 56

在上面程序中,如果具体类型不是 int,会发生什么呢?接下来看看。

package main

import (  
    "fmt"
)

func assert(i interface{}) {  
    s := i.(int) 
    fmt.Println(s)
}
func main() {  
    var s interface{} = "Steven Paul"
    assert(s)
}

在上面程序中,我们把具体类型为 strings 传递给了 assert 函数,试图从它提取出 int 值。该程序会报错:panic: interface conversion: interface {} is string, not int.

要解决该问题,我们可以使用以下语法:

v, ok := i.(T)

如果 i 的具体类型是 T,那么 v 赋值为 i 的底层值,而 ok 赋值为 true

如果 i 的具体类型不是 T,那么 ok 赋值为 falsev 赋值为 T 类型的零值,此时程序不会报错

package main

import (  
    "fmt"
)

func assert(i interface{}) {  
    v, ok := i.(int)
    fmt.Println(v, ok)
}
func main() {  
    var s interface{} = 56
    assert(s)
    var i interface{} = "Steven Paul"
    assert(i)
}

在线运行程序

当给 assert 函数传递 Steven Paul 时,由于 i 的具体类型不是 intok 赋值为 false,而 v 赋值为 0(int 的零值)。该程序打印:

56 true  
0 false

类型选择(Type Switch)

类型选择用于将接口的具体类型与很多 case 语句所指定的类型进行比较。它与一般的 switch 语句类似。唯一的区别在于类型选择指定的是类型,而一般的 switch 指定的是值。

类型选择的语法类似于类型断言。类型断言的语法是 i.(T),而对于类型选择,类型 T 由关键字 type 代替。下面看看程序是如何工作的。

package main

import (  
    "fmt"
)

func findType(i interface{}) {  
    switch i.(type) {
    case string:
        fmt.Printf("I am a string and my value is %s\n", i.(string))
    case int:
        fmt.Printf("I am an int and my value is %d\n", i.(int))
    default:
        fmt.Printf("Unknown type\n")
    }
}
func main() {  
    findType("Naveen")
    findType(77)
    findType(89.98)
}

在上述程序的第 8 行,switch i.(type) 表示一个类型选择。每个 case 语句都把 i 的具体类型和一个指定类型进行了比较。如果 case 匹配成功,会打印出相应的语句。该程序输出:

I am a string and my value is Naveen  
I am an int and my value is 77  
Unknown type

第 20 行中的 89.98 的类型是 float64,没有在 case 上匹配成功,因此最后一行打印了 Unknown type

还可以将一个类型和接口相比较。如果一个类型实现了接口,那么该类型与其实现的接口就可以互相比较

为了阐明这一点,下面写一个程序。

package main

import "fmt"

type Describer interface {  
    Describe()
}
type Person struct {  
    name string
    age  int
}

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)
}

在上面程序中,结构体 Person 实现了 Describer 接口。在第 19 行的 case 语句中,v 与接口类型 Describer 进行了比较。p 实现了 Describer,因此满足了该 case 语句,于是当程序运行到第 32 行的 findType(p) 时,程序调用了 Describe() 方法。

该程序输出:

unknown type  
Naveen R is 25 years old

接口(一)的内容到此结束。在接口(二)中我们还会继续讨论接口。祝您愉快!

若文章对你有帮助,可以点赞或打赏支持我们。发布者:Aurora,转载请注明出处:http://61.174.243.28:13541/AY-knowledg-hub/18-%e6%8e%a5%e5%8f%a3%ef%bc%88%e4%b8%80%ef%bc%89/

(0)
AuroraAurora站点维系者
上一篇 2023年 12月 5日 下午5:53
下一篇 2023年 12月 5日 下午5:55

相关推荐

  • mapfile

    文章目录mapfile概要主要用途选项参数返回值例子注意 mapfile 从标准输入读取行并赋值到数组。 概要 mapfile [-d delim] [-n count] [-O …

    入门教程 2024年 1月 3日
  • HTML CSS

    文章目录如何使用CSS内联样式HTML样式实例 – 背景颜色实例HTML 样式实例 – 字体, 字体颜色 ,字体大小实例HTML 样式实例 – …

    2023年 4月 13日
  • Java 异常处理

    异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的。 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误 java.lang.Error;如果你…

    2023年 3月 4日
  • C++ 注释

    程序的注释是解释性语句,您可以在 C++ 代码中包含注释,这将提高源代码的可读性。所有的编程语言都允许某种形式的注释。 C++ 支持单行注释和多行注释。注释中的所有字符会被 C++…

    2024年 3月 18日
  • unprotoize

    文章目录unprotoize补充说明语法选项参数 unprotoize 删除C语言源代码文件中的函数原型 补充说明 unprotoize命令 属于gcc套件,用于删除C语言源代码文…

    入门教程 2024年 3月 11日
  • HTML 编辑器

    可以使用专业的 HTML 编辑器来编辑 HTML,菜鸟教程为大家推荐几款常用的编辑器: VS Code:https://code.visualstudio.com/ Sublime…

    2023年 4月 11日
  • SQL AND & OR 运算符

    AND & OR 运算符用于基于一个以上的条件对记录进行过滤。 文章目录SQL AND & OR 运算符演示数据库AND 运算符实例实例OR 运算符实例实例结合 A…

    2023年 5月 28日
  • false

    文章目录false概要主要用途返回值注意 false 返回状态为失败。 概要 false 主要用途 用于和其他命令进行逻辑运算。 返回值 返回状态总是失败;返回值为1。 注意 该命…

    入门教程 2023年 12月 14日
  • pstree

    文章目录pstree补充说明语法选项实例 pstree 以树状图的方式展现进程之间的派生关系 补充说明 pstree命令 以树状图的方式展现进程之间的派生关系,显示效果比较直观。 …

    入门教程 2024年 3月 1日
  • lvreduce

    文章目录lvreduce补充说明语法选项参数实例 lvreduce 收缩逻辑卷空间 补充说明 lvreduce命令 用于减少LVM逻辑卷占用的空间大小。使用lvreduce命令收缩…

    入门教程 2023年 12月 19日
Translate »