golang中的实现接口

我想实现如下所示的接口。我不知道如何开始。有人可以告诉我应该如何实现这些功能吗?


package interval

package main


type Interval interface {

    contains(r float64) bool // if r is in x, then true

    average(Y Intervall) (Intervall, error)

    String() string                    //cast interval"[a,b]" to [a,b]

    completecontains(Y Intervall) bool //if y is completely in x, give true

    New(a, b float64) Intervall

    //var a int

}

type Complex struct {

    first int

}


func (c Complex) contains(r float64) bool {

    if a <= r <= b {

        return true

    } else {

        return false

    }

}


func (c Complex) String() string {

    return "a"

}


func (c Complex) length() float64 {

    return 2.3

}


func main() {

}


ibeautiful
浏览 191回答 2
2回答

绝地无双

我真的不知道你在这里真正想做什么,但是代码有几个问题a 和 b 未定义,我将它们添加到 complex 以使其编译a <= r <= b 在 go 中无效,更改为你有一个主要的,所以我假设你的意思是这是一个可运行的应用程序。包需要被称为“main”才能直接运行。可能不是你想要的,但它现在编译并运行(但不做任何事情,因为 main 是空的)package main//import "fmt"type Intervall interface {&nbsp; &nbsp; contains(r float64) bool // if r is in x, then true&nbsp; &nbsp; average(Y Intervall) (Intervall, error)&nbsp; &nbsp; String() string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //cast interval"[a,b]" to [a,b]&nbsp; &nbsp; completecontains(Y Intervall) bool //if y is completely in x, give true&nbsp; &nbsp; New(a, b float64) Intervall}type Complex struct {&nbsp; &nbsp; first int&nbsp; &nbsp; a&nbsp; &nbsp; &nbsp;float64&nbsp; &nbsp; b&nbsp; &nbsp; &nbsp;float64}func (c Complex) contains(r float64) bool {&nbsp; &nbsp; if c.a <= r && r <= c.b {&nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; }}func (c Complex) String() string {&nbsp; &nbsp; return "a"}func (c Complex) length() float64 {&nbsp; &nbsp; return 2.3}func main() {}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go