接口方法返回自身类型的值

huangapple go评论72阅读模式
英文:

Interface method return value of own type

问题

我正在尝试创建一个方法,该方法将接受特定类型的结构体并对其进行操作。然而,我需要一个可以在结构体实例上调用的方法,并且它将返回该结构体类型的对象。我遇到了一个编译时错误,因为实现接口的类型的返回类型与接口的方法返回类型不同,但这是因为接口需要返回其自身类型的值。

接口声明:

type GraphNode interface {
    Children() []GraphNode
    IsGoal() bool
    GetParent() GraphNode
    SetParent(GraphNode) GraphNode
    GetDepth() float64
    Key() interface{}
}

实现该接口的类型:

type Node struct {
    contents []int
    parent   *Node
    lock     *sync.Mutex
}

func (rootNode *Node) Children() []*Node {
    ...
}

错误信息:

.\astar_test.go:11: cannot use testNode (type *permutation.Node) as type GraphNode in argument to testGraph.GetGoal:
*permutation.Node does not implement GraphNode (wrong type for Children method)
have Children() []*permutation.Node
want Children() []GraphNode

获取父节点的方法:

func (node *Node) GetParent() *Node {
    return node.parent
}

上述方法失败,因为它返回一个指向节点的指针,而接口返回类型为GraphNode。

英文:

I am trying to make a method which will take structs of a certain type and do operations on them. However, I need to have a method one can call on an instance of the stuct, and it will return objects of that struct's type. I am getting a compile time error because the return type of the type which implements the interface isn't the same as the interface's method return type, but that's because the interface needs to return values of it's own type.

Interface declaration:

type GraphNode interface {
	Children() []GraphNode
	IsGoal() bool
	GetParent() GraphNode
	SetParent(GraphNode) GraphNode
	GetDepth() float64
	Key() interface{}
}

Type which implements that interface:

type Node struct {
	contents []int
	parent   *Node
	lock     *sync.Mutex
}

func (rootNode *Node) Children() []*Node {
...
}

Error Message:

.\astar_test.go:11: cannot use testNode (type *permutation.Node) as type GraphNode in argument to testGraph.GetGoal:
*permutation.Node does not implement GraphNode (wrong type for Children method)
have Children() []*permutation.Node
want Children() []GraphNode

Method to get parent:

func (node *Node) GetParent() *Node {
	return node.parent
}

The above method fails because it returns a pointer to a node, and the interface returns type GraphNode.

答案1

得分: 3

*Node没有实现GraphNode接口,因为Children()的返回类型与接口中定义的类型不同。即使*Node实现了GraphNode,你也不能在期望[]GraphNode的地方使用[]*Node。你需要声明Children()返回[]GraphNode。类型为[]GraphNode的切片元素可以是*Node类型。

对于GetParent(),只需将其更改为以下内容:

func (node *Node) GetParent() GraphNode {
    return node.parent
}
英文:

*Node doesn't implement the GraphNode interface because the return type of Children() isn't the same as that defined in the interface. Even if *Node implements GraphNode, you can't use []*Node where []GraphNode is expected. You need to declare Children() to return []GraphNode. The elements of a slice of type []GraphNode can be of type *Node.

For GetParent(), just change it to this:

func (node *Node) GetParent() GraphNode {
    return node.parent
}

huangapple
  • 本文由 发表于 2017年3月6日 07:15:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/42615210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定