【问题标题】:implementation interfaces in golanggolang中的实现接口
【发布时间】:2015-04-03 20:25:53
【问题描述】:

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

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() {
}

【问题讨论】:

  • 如果你想让Complex成为Interval,它需要实现接口定义中描述的所有功能。你有什么问题?
  • 好的,欢迎堆栈溢出。此链接提供有关如何提出好问题的帮助:stackoverflow.com/help/how-to-ask
  • 不接受篡改您的问题。

标签: object interface go lang


【解决方案1】:

我无法确定您在此处实际尝试做什么,但代码存在一些问题

  1. a 和 b 未定义,我将它们添加到 complex 以使其编译
  2. a
  3. 您有一个主应用程序,所以我假设您的意思是它是可运行的应用程序。包需要被称为“main”才能直接运行。

可能不是你想要的,但它现在可以编译并运行(但由于 main 为空,所以什么都不做)

Here it is on play

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() {

}

【讨论】:

  • 我想实现 x 和 y 的平均值。我应该如何实现它
【解决方案2】:

不确定为什么将具体间隔称为“复杂”或两个间隔的平均值可能是多少,但这是我能得到的最接近的值。另外,不确定在这里使用接口有什么好处。

http://play.golang.org/p/sxFRkJZCFa

package main

import "fmt"

type Interval interface {
    Contains(r float64) bool
    Average(y Interval) (Interval, error)
    String() string
    CompletelyContains(y Interval) bool
    CompletelyContainedBy(y Interval) bool
}

type Complex struct {
    a, b float64
}

func (c Complex) Contains(r float64) bool {
    return c.a <= r && r <= c.b
}

func (c Complex) Average(y Interval) (Interval, error) {
    return nil, fmt.Errorf("What the heck is the average of two intervals?")
}

func (c Complex) CompletelyContains(y Interval) bool {
    return y.CompletelyContainedBy(c)
}

func (c Complex) CompletelyContainedBy(y Interval) bool {
    return y.Contains(c.a) && y.Contains(c.b)
}

func (c Complex) String() string {
    return fmt.Sprintf("[%v,%v]", c.a, c.b)
}

func main() {
    var x Interval = Complex{a: 1, b: 5.1}
    var y Interval = Complex{a: 1.3, b: 5}
    fmt.Println("x contains 3:", x.Contains(3))
    fmt.Println("x completely contains y:", x.CompletelyContains(y))

    avg, err := x.Average(y)
    fmt.Println("Average of x and y:", avg, "with error:", err)

    fmt.Println("x:", x)

}

编辑:这是一种以您想要的方式实现“平均”的愚蠢复杂的方法。复杂性来自于避免直接访问 y.a 和 y.b,这会破坏使用接口(如果有的话)的目的。

http://play.golang.org/p/Tc5YCciLWq

【讨论】:

  • 好的。我的回答是否有任何帮助或令人困惑的方面?
  • 我找不到任何以间隔表示的两个间隔的平均值的参考。你的意思是,给定 [1,3] 和 [6,9],你想要 [4,5]?
  • 你关心开区间和闭区间吗?从技术上讲,[1,3] 和 [6,9] 之间是 (3,6)。给出 [3,6] 作为答案更容易,但您可以创建一组表示打开、关闭、半打开等间隔的类型以使其更精确。
猜你喜欢
  • 2015-09-30
  • 2019-01-09
  • 2017-11-04
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-10
相关资源
最近更新 更多