我想实现如下所示的界面。我不知道如何开始。谁能告诉我应该如何实现这些功能?
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() {
}
最佳答案
我真的不知道你在这里到底想做什么,但是代码有几个问题
- a 和 b 未定义,我将它们添加到 complex 以使其编译
- a <= r <= b 在 go 中无效,已更改
- 你有一个主应用程序,所以我假设你的意思是它是可运行的应用程序。包需要被称为“main”才能直接运行。
可能不是你想要的,但它现在编译并运行(但不做任何事情,因为 main 是空的)
package main
//import "fmt"
type Intervall 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
}
type Complex struct {
first int
a float64
b float64
}
func (c Complex) contains(r float64) bool {
if c.a <= r && r <= c.b {
return true
} else {
return false
}
}
func (c Complex) String() string {
return "a"
}
func (c Complex) length() float64 {
return 2.3
}
func main() {
}
关于object - golang中的实现接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29439208/