栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

可比接口称为什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

可比接口称为什么?

编辑:
考虑此用户类型:

type userType struct {    frequency int    value     rune}

并假设您想将此类型添加到“链接列表”:
并且应首先按频率对其进行排序,然后,如果频率相同,请查看char值。因此该

Compare
函数将是:

func (a userType) Compare(b userType) int {    if a.frequency > b.frequency {        return 1    }    if a.frequency < b.frequency {        return -1    }    if a.value > b.value {        return 1    }    if a.value < b.value {        return -1    }    return 0}

满足以下接口:

type Comparer interface {    Compare(b userType) int}

现在将这些

{1,'d'} {2,'b'} {3,'c'} {4,'a'} {4,'b'} {4,'c'}
类型添加到linkeList:
示例代码:

package mainimport (    "container/list"    "fmt")type Comparer interface {    Compare(b userType) int}type userType struct {    frequency int    value     rune}// it should sort by frequency first, then if the frequencies are the same, look at the char value.func (a userType) Compare(b userType) int {    if a.frequency > b.frequency {        return 1    }    if a.frequency < b.frequency {        return -1    }    if a.value > b.value {        return 1    }    if a.value < b.value {        return -1    }    return 0}func Insert(val userType, l *list.List) {    e := l.Front()    if e == nil {        l.PushFront(val)        return    }    for ; e != nil; e = e.Next() {        var ut userType = e.Value.(userType)        if val.Compare(ut) < 0 { l.InsertBefore(val, e) return        }    }    l.PushBack(val)}func main() {    l := list.New()    Insert(userType{4, 'c'}, l)    Insert(userType{4, 'a'}, l)    Insert(userType{4, 'b'}, l)    Insert(userType{2, 'b'}, l)    Insert(userType{3, 'c'}, l)    Insert(userType{1, 'd'}, l)    for e := l.Front(); e != nil; e = e.Next() {        ut := e.Value.(userType)        fmt.Printf("{%d,%q} ", ut.frequency, ut.value)    }    fmt.Println()    var t interface{} = userType{4, 'c'}    i, ok := t.(Comparer)    fmt.Println(i, ok)}

并输出:

{1,'d'} {2,'b'} {3,'c'} {4,'a'} {4,'b'} {4,'c'} {4 99} true

因此,如果您准备使用已知类型(例如

int
),请参见以下示例:

package mainimport (    "container/list"    "fmt")func Insert(val int, l *list.List) {    e := l.Front()    if e == nil {        l.PushFront(val)        return    }    for ; e != nil; e = e.Next() {        v := e.Value.(int)        if val < v { l.InsertBefore(val, e) return        }    }    l.PushBack(val)}func main() {    l := list.New()    Insert(4, l)    Insert(2, l)    Insert(3, l)    Insert(1, l)    for e := l.Front(); e != nil; e = e.Next() {        fmt.Print(e.Value, " ") // 1 2 3 4    }    fmt.Println()}

旧:

Go中没有这样的界面。您可以编写此

Less
函数来比较您的类型:

func Less(a, b interface{}) bool {    switch a.(type) {    case int:        if ai, ok := a.(int); ok { if bi, ok := b.(int); ok {     return ai < bi }        }    case string:        if ai, ok := a.(string); ok { if bi, ok := b.(string); ok {     return ai < bi }        }    // ...    default:        panic("Unknown")    }    return false}

测试示例代码:

package mainimport (    "container/list"    "fmt")func Less(a, b interface{}) bool {    switch a.(type) {    case int:        if ai, ok := a.(int); ok { if bi, ok := b.(int); ok {     return ai < bi }        }    case string:        if ai, ok := a.(string); ok { if bi, ok := b.(string); ok {     return ai < bi }        }    default:        panic("Unknown")    }    return false}func Insert(val interface{}, l *list.List) *list.Element {    e := l.Front()    if e == nil {        return l.PushFront(val)    }    for ; e != nil; e = e.Next() {        if Less(val, e.Value) { return l.InsertBefore(val, e)        }    }    return l.PushBack(val)}func main() {    l := list.New()    Insert(4, l)    Insert(2, l)    Insert(3, l)    Insert(1, l)    for e := l.Front(); e != nil; e = e.Next() {        fmt.Print(e.Value, " ")    }    fmt.Println()    Insert("C", l)    Insert("A", l)    Insert("AB", l)    Insert("C", l)    Insert("C2", l)    Insert("C1", l)    for e := l.Front(); e != nil; e = e.Next() {        fmt.Print(e.Value, " ")    }    fmt.Println()}

输出:

1 2 3 4 1 2 3 4 A AB C C C1 C2


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/483206.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号