go之接口

Go语言接口与多态实践
本文通过示例介绍Go语言中接口的概念及其应用,包括如何定义接口、实现多态等,并展示了不同类型如何通过实现相同的接口来达到多态的效果。
  • 接口不能包含变量,只能包含一组抽象的方法集合。
  • 类型实现了接口中的所有抽象方法,则该类型为接口的实现类
  • 多个类型可以实现同一个接口,一个类型可以实现多个接口
  • 接口可以嵌套接口
  • 定义
type IA interface {
	AFun()
}
  • 多态
type Income interface {  
    calculate() int
    source() string
}

type FixedBilling struct {  
    projectName string
    biddedAmount int
}
type TimeAndMaterial struct {  
    projectName string
    noOfHours  int
    hourlyRate int
}
type Advertisement struct {  
    adName     string
    CPC        int
    noOfClicks int
}
func (fb FixedBilling) calculate() int {  
    return fb.biddedAmount
}
func (fb FixedBilling) source() string {  
    return fb.projectName
}
func (tm TimeAndMaterial) calculate() int {  
    return tm.noOfHours * tm.hourlyRate
}
func (tm TimeAndMaterial) source() string {  
    return tm.projectName
}
func calculateNetIncome(ic []Income) {  
    var netincome int = 0
    for _, income := range ic {
        fmt.Printf("Income From %s = $%d\n", income.source(), income.calculate())
        netincome += income.calculate()
    }
    fmt.Printf("Net income of organisation = $%d", netincome)
}
func main() {  
    project1 := FixedBilling{projectName: "Project 1", biddedAmount: 5000}
    project2 := FixedBilling{projectName: "Project 2", biddedAmount: 10000}
    project3 := TimeAndMaterial{projectName: "Project 3", noOfHours: 160, hourlyRate: 25}
    bannerAd := Advertisement{adName: "Banner Ad", CPC: 2, noOfClicks: 500}
    popupAd := Advertisement{adName: "Popup Ad", CPC: 5, noOfClicks: 750}
    incomeStreams := []Income{project1, project2, project3, bannerAd, popupAd}
    calculateNetIncome(incomeStreams)
}


类型 *T 的可调用方法集包含接受者为 *T 或 T 的所有方法集
类型 T 的可调用方法集包含接受者为 T 的所有方法
类型 T 的可调用方法集不包含接受者为 *T 的方法
  • 泛型
1。使用类型断言 
2。Go语言反射功能 
3。自定义类型接口和类型


package main
import (
	"fmt"
	"reflect"
)

func max(first interface{}, rest ...interface{}) interface{} {
	max := first
	for _, v := range rest {
		switch v := v.(type) {
		case int:
			if v > max.(int) {
				max = v
			}
		case float64:
			if v > max.(float64) {
				max = v
			}
		}
	}
	return max
}

func main() {
	fmt.Println(max(1,2 ,3, 6, 7, 8))
	fmt.Println(max(2.1, 3.4, 6.7, 9.1))
}

package main
import (
"fmt"
"reflect"
)

func index(slice interface{}, v interface{}) int {
	if slice := reflect.ValueOf(slice); slice.Kind() == reflect.Slice {
		for i := 0; i < slice.Len(); i++ {
			if reflect.DeepEqual(v, slice.Index(i).Interface()){
				return i
			}
		}
	}
	return -1
}

func main() {
	slice1 := []int{0, 4, 5, 7, 9 }
	fmt.Println(index(slice1, 7))

	slice2 := []float64{1.1, 7.8, 3.4, 9.0 }
	fmt.Println(index(slice2, 9.0))

	slice3 := []interface{}{0, 3, 4, 5, "abc", 6 }
	fmt.Println(index(slice3, "abc"))
}

package main
import(
"fmt"
)
//接口
type MyType interface {
	Equal(a int, b interface{}) bool
	Len() int
}

//返回切片下标位置
func index(slice MyType, b interface{}) int {
	for i := 0; i < slice.Len(); i++ {
		if slice.Equal(i, b) {
			return i
		}
	}
	return -1
}

//自定义类型IntType实现MyType接口
type IntType []int
func (slice IntType) Equal(i int, b interface{}) bool {
	return slice[i] == b.(int)
}
func (slice IntType) Len() int {
	return len(slice)
}

//自定义类型FloatType实现MyType接口
type FloatType []float64
func (slice FloatType) Equal(i int, b interface{}) bool {
	return slice[i] == b.(float64)
}
func (slice FloatType) Len() int {
	return len(slice)
}


func main() {
	slice := IntType {1, 2, 3, 4, 5 }
	fmt.Println(index(slice, 5))

	slice2 := FloatType{1.2, 6.2, 3.4, 7.8}
	fmt.Println(index(slice2, 7.8))
}

 

转载于:https://my.oschina.net/yangting880815/blog/2050263

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值