dsbifvcxr458755493 2012-11-08 01:36
浏览 50
已采纳

使用定义中的接口的Go接口

I am playing around with making a Fibonacci Heap. (They are mentioned several times in an algorithms class that I'm taking, and I want to check them out.) I want the heap to use nodes of any kind, so I define a Node interface:

package node

type Node interface {
    AddChild(other Node)
    Less(other Node) bool
}

type NodeList []Node

func (n NodeList) AddNode(a Node) {
    n = append(n, a)
}

(I use the []Node array because it has the same affect as the heap definition.) As you can see, the Node interface defines its two functions with arguments of type Node. This should mean that the functions have to accept arguments that implement the Node interface. The rest of the heap uses these nodes.

In the program that uses this heap, I make a type that implements the Node interface:

package main

import "container/list"
import node "./node"

type Element struct {
    Children *list.List
    Value int
}

func (e Element) AddChild(f Element) {
    e.Children.PushBack(f)
}

func (e Element) Less(f Element) bool {
    return e.Value < f.Value
}

func main() {
    a := Element{list.New(), 1}

    n := new(node.NodeList)
    n.AddNode(a)
}

However, this didn't work. The compiler complains that Element doesn't have the correct function definitions for the interface.

cannot use a (type Element) as type node.Node in function argument:
Element does not implement node.Node (wrong type for AddChild method)
    have AddChild(Element)
    want AddChild(node.Node)

What is wrong here? Obviously Element doesn't implement the interface correctly, but I think it's because of how I defined the interface. Is there a correct way to do what I want in Go? Can interfaces refer to themselves?

  • 写回答

1条回答 默认 最新

  • 普通网友 2012-11-08 01:45
    关注

    The function

    func (e Element) Less(f Element) bool
    

    does not match the function from the interface

    func Less(other Node) bool
    

    You need to actually match the signature, as in

    func (e Element) Less(f Node) bool
    

    And yes, this means you could be passed a Node that isn't an Element. You'll have to test for that at runtime and panic.


    As an example for why this is so, consider if your code was legit, and I tried the following:

    type Other int
    func (o Other) Less(f Other) bool {
        return o < f
    }
    func (o Other) AddChild(f Other) {}
    e = GetSomeElement() // of type Element
    var o Other
    var n Node = e
    fmt.Println(n.Less(o))
    

    Because I stored the Element into a var of type Node, I can now call Less() with an argument that isn't another Element, which violates the type of Element.Less(). This is why that's not legal.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图
  • ¥15 arduino双向交通灯设计
  • ¥15 有没有会粒子群算法的大能(○゜ε^○)求带不会出收敛图😭
  • ¥15 Matlab读取根元素出错