接口方法引用相同的接口?

发布于 2025-01-16 06:26:59 字数 606 浏览 1 评论 0原文

我是一名java程序员,我现在正在学习go。 我尝试编写一个名为 Vo 的自嵌套接口,以及一个名为 Score 的 Vo 实现, 像这样的代码:

type Vo interface {
        Merge(v Vo) Vo
}

type Score int

func (this Score) Merge(s Score) Score {
    return Score(this + s)
}

func TestThePkg(t *testing.T) {
    s := Score(5)
    var v Vo = s //compile error
}

详细的编译错误是:

cannot use s (variable of type Score) as Vo value in variable declaration: Score does not implement Vo (wrong type for method Merge)
        have Merge(s Score) Score
        want Merge(v Vo) VocompilerInvalidIfaceAssign

如何解决这个问题? 谢谢!

i'm a java programer ,i'm learing go now.
i try to wirte a self nested interface named Vo,and a implemention of Vo named Score,
code like this:

type Vo interface {
        Merge(v Vo) Vo
}

type Score int

func (this Score) Merge(s Score) Score {
    return Score(this + s)
}

func TestThePkg(t *testing.T) {
    s := Score(5)
    var v Vo = s //compile error
}

detail compile error is :

cannot use s (variable of type Score) as Vo value in variable declaration: Score does not implement Vo (wrong type for method Merge)
        have Merge(s Score) Score
        want Merge(v Vo) VocompilerInvalidIfaceAssign

how to resolve this problem?
thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此刻的回忆 2025-01-23 06:26:59

游乐场

func (this Score) Merge(s Vo) Vo {
    if typeAssersion, ok := s.(Score); ok {
        return Score(this + typeAssersion)
    }
    //log err or the `Vo` interface should return (Vo,error)
    return nil
}

play ground

func (this Score) Merge(s Vo) Vo {
    if typeAssersion, ok := s.(Score); ok {
        return Score(this + typeAssersion)
    }
    //log err or the `Vo` interface should return (Vo,error)
    return nil
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文