zoukankan      html  css  js  c++  java
  • Golang教程:接口

    什么是接口

    在面向对象语言中,接口一般被定义为 :接口定义了一个对象的行为。它仅仅指定了一个对象应该做什么。具体怎么做(实现细节)是由对象决定的。

    在 Go 中,一个接口定义为若干方法的签名。当一个类型定义了所有接口里的方法时,就说这个类型实现了这个接口。这和 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行我们添加了一个方法 FindVowels() []rune 给接受者类型 MyString。现在可以说 MyString 实现了 VowelsFinder 接口。这和其他语言大大的不同,比如在Java中,一个类必须用 implements 关键字显式的标明实现了一个接口。这在Go中是不需要的,在Go中,如果一个类型包含了一个接口声明的所有方法,那么这个类型就隐式地实现了这个接口。

    在第28行,我们将 MyString 类型的变量 name 赋值给 VowelsFinder 类型的变量 v。这是合法的,因为 MyString 实现了 VowelsFinder。在下一行,v.FindVowels() 在 MyString 上调用 FindVowels 方法打印在 Sam Anderson 中所有的元音。程序的输出为:Vowels are [a e o]

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

    接口的实际用途

    上面的程序告诉我们怎么创建和实现接口,但是没有展示接口的实际用途。在上面的程序中,我们可以使用 name.FindVowels()替代 v.FindVowels(),程序照样可以工作,那么我们为什么还要使用接口呢?

    现在让我们来看接口的一个实际用途。

    我们将编写一个简单的程序,根据员工的个人工资计算公司的总支出。为了简洁起见,我们假设所有费用都是美元。

    package main
    
    import (  
        "fmt"
    )
    
    type SalaryCalculator interface {  
        CalculateSalary() int
    }
    
    type Permanent struct {  
        empId    int
        basicpay int
        pf       int
    }
    
    type Contract struct {  
        empId  int
        basicpay int
    }
    
    //salary of permanent employee is sum of basic pay and pf
    func (p Permanent) CalculateSalary() int {  
        return p.basicpay + p.pf
    }
    
    //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

    在公司中我们有两种类型的员工:Permanent 和 Contract (第 11 和 17 行)。永久性员工的工资是 basicpay 和 pf 的总和,而合同员工的工资只是基本工资 basicpay。这分别在第23行和第28行的方法 CalculateSalary 中表示。通过声明这个方法,Permanent 和 Contract 都实现了 SalaryCalculator 接口。

    第36行中, totalExpense 方法的声明展示了使用接口的好处。这个方法接收一个 SalaryCalculator 接口的切片 []SalaryCalculator 作为参数。在第49行,我们将一个包含了 Permanent 和 Contract 类型的切片给方法 totalExpensetotalExpense 方法出通过调用各自类型的 CalculateSalary 方法来计算总支。这是在第 39行完成的。

    最大的优点是可以将 totalExpense 扩展到任何新的员工类型,而不必修改任何代码。假设该公司引入了第三种员工类型 Freelancer 和不同的计算工资的方法。那么 Freelancer 可以被包含在传递给 totalExpense 的切片参数中,而不需要修改任何 totalExpense 方法的接口。这个方法会做自己应该做的事情,因为 Freelancer 同样实现了 SalaryCalculator 接口:)

    程序的输出为:Total Expense Per Month $14050

    接口的内部表示

    可以把接口想象成这样一个元组 (type, value)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
    ", t, t)
    }
    
    func main() {  
        var t Test
        f := MyFloat(89.7)
        t = f
        describe(t)
        t.Tester()
    }

    Test 接口提供了一个方法 Tester()MyFloat 类型实现了这个接口。在第 24 行,我们将 MyFloat 类型的变量 f 赋值给 Test 类型的变量 t 。现在 t 的具体类型是 MyFloat 而它的值是 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
    ", 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行,我们传递 string,int 和结构体给 describe 。程序的输出结果为:

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

    类型断言

    类型断言(type assertion)用来提取接口的实际类型的值。

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

    在上面的程序中,我们将实际类型为 string 的变量 s 传递给 assert 函数,assert 函数尝试从其中提取出一个 int值。该程序会触发 panic:panic: interface conversion: interface {} is string, not int

    为了解决以上问题,我们可以使用下面的语法:

    v, ok := i.(T)

    如果 i 的具体类型是 T,则 v 将具有 i 的实际值,ok 为 true

    如果 i 的具体类型不是 T,则 ok 将为 false, v 将具有 T 的 0 值,程序不会触发 panic。

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

    当 Steven Paul 传递给 assert 函数,ok 将是 false 因为 i 的实际类型不是 int 并且 v 的值将是 0(int 的 0 值)。程序将输出:

    56 true  
    0 false

    Type Switch

    类型分支(type switch)用来将一个接口的具体类型与多个 case 语句指定的类型进行比较。这很像普通的 switch 语句。唯一不同的是 type switch 中 case 指定的是类型,而普通的 switch 语句中 case 指定的是值。

    type switch 的语法与类型断言和很相似。在类型断言 i.(T) 中,将类型 T 替换为关键字 type 就变成了 type switch。让我们通过下面的程序看看它是如何工作的。

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

    上面的程序中,第8行,switch i.(type) 是一个 type switch。每一个 case 语句都将 i 的实际类型和 case 指定的类型相比较。如果一个 case 匹配,则打印相应的语句。程序的输出为:

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

    在第20行,89.98 是 flaot64 类型,并不匹配任何一个 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
    ")
        }
    }
    
    func main() {  
        findType("Naveen")
        p := Person{
            name: "Naveen R",
            age:  25,
        }
        findType(p)
    }

    在上面的程序中,Person 结构体实现了 Describer 接口。在第 19 行的 case 语句,v 和 Describe 接口进行比较。由于 p 实现了 Describer 接口因此这个 case 匹配成功,在第32行 findType(p) 中 Person 的 Describe() 方法被调用。

    程序的输出为:

    unknown type  
    Naveen R is 25 years old  
  • 相关阅读:
    软件测试——一点看法
    软件测试——注意事项
    软件测试——Peer Review
    软件测试——白盒测试
    软件测试——闰年检测程序及异常问题解决
    JS动态,有选择性的改变div颜色
    软件测试——EditBox等价类划分扩展
    软件测试——EditBox等价类划分
    初窥软件测试
    开博第一篇
  • 原文地址:https://www.cnblogs.com/liuzhongchao/p/9186333.html
Copyright © 2011-2022 走看看