Golang: when typecasting child struct to parent struct, is child struct information lost?

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

Golang: when typecasting child struct to parent struct, is child struct information lost?

问题

例如,在子结构中嵌入父结构后:

type ParentNode struct {
}

type ChildNode struct {
    ParentNode
    Ident string
}

func ParentType() ParentNode { 
    child := ChildNode{Ident: "node"}
    fmt.Println(child)
    return child.ParentNode 
}

func main() {
    x := ParentType()
    fmt.Println(x.Ident)
}

这段代码会打印出 "node",并返回包含所有信息的子结构包装在父结构中,这样我们就可以在操作表面上的父结构时实际上操作的是子结构。这个想法类似于Java中可以返回一个表面类型为List但实际类型为LinkedList的情况。

如果不行,有什么最好的方法可以实现这个功能?我想将子结构向上转型为父结构,但在操作时将其视为子结构。是否可以使用接口来解决这个问题?

如何消除 fmt.Println(x.Ident) 行的错误 "x.Ident undefined (type ParentNode has no field or method Ident)"?

英文:

For example after embedding the parent struct in the child struct:

type ParentNode struct {
}

type ChildNode struct {
    ParentNode
    Ident string
}

func ParentType() ParentNode { 
    child := ChildNode{Ident : "node"}
    fmt.Println(child)
    return child.ParentNode 
}

func main() {
    x := ParentType()
	fmt.Println(x.Ident)

}

Would this print out "node" and also return the child struct wrapped in the parent struct with all the information, such that we can manipulate the apparent parent struct while having the actual child struct? The idea for this is similar to Java where you can return an apparent type of List but return an actual type of LinkedList.

If not, what would be the best way to achieve this functionality? Essentially I want to upcast the Child struct to a parent struct but manipulate it as if it was a child struct. Is there a way of solving this using an interface?

How is it possible to get rid of error "x.Ident undefined (type ParentNode has no field or method Ident)" at line fmt.Println(x.Ident)

答案1

得分: 2

它不会返回子结构体。你所做的不是将子结构体进行类型转换,而只是返回其中的一个字段。举个更具体的例子,上面的Go代码片段就像是试图将一个Employee的名字当作Employee的超类来使用。

你的猜测是正确的,实现类似ArrayList和List的“是一个”关系的方法是使用接口。不过需要注意的是,接口只能提供方法调用的多态性,而不能提供字段访问的多态性。你可以在以下链接中找到一个修改过的版本的示例,希望对你有所帮助:

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

阅读Go语言规范中的“结构体类型”部分,或者整个规范(它并不长!也不可怕!)可能会对你有帮助:

https://golang.org/ref/spec#Struct_types

英文:

It would not return the child struct. What you are doing is not typecasting the child struct, but just returning one of its fields. To use a more concrete example, the above go snippet would be like trying to use the name of an Employee as if it were a super class of Employee.

Your guess was right, though, that the way to achieve the "is-a" relationship like ArrayList and List is to use an interface. Know, though, that interfaces can only provide polymorphism for method calls, not field access. A modified version of your example, which is hopefully helpful, can be found at:

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

You would likely find it helpful to read the "Struct Types" portion of the go spec, and/or the whole spec (it's not that long! Or scary!):

https://golang.org/ref/spec#Struct_types

huangapple
  • 本文由 发表于 2015年11月15日 11:31:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/33716109.html
匿名

发表评论

匿名网友

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

确定