• Golang之interface(多态,类型断言)


    多态用法

    package main
    
    //一种事物的多种形态,都可以按照统一的接口进行操作
    //多态
    import (
        "fmt"
        "math/rand"
        "sort"
    )
    
    type Student struct {
        Name     string
        Id       string
        Age      int
        sortType int
    }
    type Book struct {
        Name   string
        Author string
    }
    
    //切片默认传地址
    type StudentArray []Student
    
    func (p StudentArray) Len() int {
        return len(p)
    }
    
    func (p StudentArray) Less(i, j int) bool {
        return p[i].Name < p[j].Name
    }
    
    func (p StudentArray) Swap(i, j int) {
        p[i], p[j] = p[j], p[i]
    }
    
    func main() {
        var stus StudentArray
        for i := 0; i < 10; i++ {
            stu := Student{
                Name: fmt.Sprintf("stu%d", rand.Intn(100)),
                Id:   fmt.Sprintf("110%d", rand.Int()),
                Age:  rand.Intn(100),
            }
            stus = append(stus, stu)
        }
        for _, v := range stus {
            fmt.Println(v)
        }
    
        fmt.Println("
    
    ")
        sort.Sort(stus)
        for _, v := range stus {
            fmt.Println(v)
        }
    }

     接口嵌套

    package main
    
    import "fmt"
    //接口嵌套 一个接口可以嵌套在另外的接口
    type Reader interface {
        Read()
    }
    type Writer interface {
        Write()
    }
    type ReadWriter interface {
        Reader
        Writer
    }
    type File struct {
    }
    
    func (f *File) Read() {
        fmt.Println("read data")
    }
    
    func (f *File) Write() {
        fmt.Print("write data")
    }
    func Test(rw ReadWriter) {
        rw.Read()
        rw.Write()
    }
    
    func main() {
        var f File
        Test(&f)
    }

     类型断言

    package main
    
    import "fmt"
    
    type Student struct {
        Name string
        Sex  string
    }
    //类型断言
    //一个判断传入参数类型的函数
    func just(items ...interface{}) {
        for index, v := range items {
            switch v.(type) {
            case bool:
                fmt.Printf("%d params is bool,value is %v
    ", index, v)
            case int, int64, int32:
                fmt.Printf("%d params is int,value is %v
    ", index, v)
            case float32, float64:
                fmt.Printf("%d params is float,value is %v
    ", index, v)
            case string:
                fmt.Printf("%d params is string,value is %v
    ", index, v)
            case Student:
                fmt.Printf("%d params student,value is %v
    ", index, v)
            case *Student:
                fmt.Printf("%d params *student,value is %v
    ", index, v)
    
            }
        }
    }
    func main() {
        var b Student = Student{
            Name: "stu01",
            Sex:  "female",
        }
        just(28, 8.2, "this is a test", b, &b)
    
    }
  • 相关阅读:
    德国10马克,高斯正态分布函数
    安装python的第三方库 geopandas
    Python版本的GDAL 安装
    [原创]App崩溃率统计工具推荐
    用户增长模型AARRR模型
    [原创]nginx日志分析工具
    [原创]浅谈移动互联网创业公司工具类产品
    [原创]浅谈在创业公司对PMF的理解
    [原创]浅谈创业公司如何选择产品方向
    [原创]浅谈在创业公司对MVP的理解
  • 原文地址:https://www.cnblogs.com/pyyu/p/8283418.html
Copyright © 2020-2023  润新知