object - golang中的实现接口(interface)

标签 object interface go lang

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

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

最佳答案

我真的不知道你在这里到底想做什么,但是代码有几个问题

  1. a 和 b 未定义,我将它们添加到 complex 以使其编译
  2. a <= r <= b 在 go 中无效,已更改
  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() {

}

关于object - golang中的实现接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29439208/

相关文章:

c# - 为什么接口(interface)不是 [Serializable]?

指定类型时 javac "uses unchecked or unsafe operations"

java - 如何在不将 null 作为输出的情况下打印 getter String?

javascript - 如何在 javascript 中对多个 HTMLElement 事件使用相同的函数?

c# - 访问中继器中的对象数据

go - 运行时错误: invalid memory address or nil pointer dereference, grpc golang例子

go - 如何为 CRUD 模型创建通用接口(interface)?

javascript - 如何在 vue.js 中不使用循环方法将类型数组对象更改为格式化对象?

c# - 为什么我需要在 C# 显式实现中将 'this' 转换为接口(interface)类型?

json - 如何读取结构字段 ` ` 装饰器?