在go中是否存在实现虚拟函数行为的技巧?我有下面的例子。
package main
import "fmt"
type A struct {
}
type B struct {
*A
}
func (this *A) Do() {
fmt.Println("IM DO")
this.DoVirtual()
}
func (this *A) DoVirtual() {
fmt.Println("IM DoVirtual Default implementation")
}
func (this *B) DoVirtual() {
fmt.Println("IM DoVirtual B implementation")
}
func main() {
a := &A{}
a.Do()
fmt.Println("----")
b := &B{}
b.DoVirtual()
b.Do() // How to make here B.DoVirtual call
}最后一个Do()调用也使用默认的DoVirtual实现,实际上不是我想要的。原因是go lang继承模型:在这种情况下,这个*B和这个*A是不同的指针,但我想知道是否可以对这种行为进行一些模拟,在最后一次调用中,DoVirtual将从B类中使用。
发布于 2016-04-27 09:16:24
当编译器选择func (this *A) Do() {时,封闭B已经消失,不再可访问。Go不支持is-a结构,只支持has(组合)。如果实现了一个func (this *B) Do() {,它将取代*A上的Do(),当然,这并不是您想要的。
我认为对实施和动机的最好解释是来自Rob的越少越好。。
发布于 2016-04-27 09:17:41
我的简单模拟,您可以通过一些reflect,代码生成,甚至RPC来改进它。其主要思想是添加一个虚拟方法表,并像c++那样自行分派。
package main
import "fmt"
type A struct {
virFunDispatcher func(funAndParams ... interface{})
}
func newA() *A{
return &A{func(funAndParams ... interface{}){
fun := funAndParams[0].(string)
switch(fun){
case "DoVirtual":
fmt.Println("IM DoVirtual Default implementation")
}
}}
}
type B struct {
*A
}
func newB() *B{
a := A{func(funAndParams ... interface{}){
fun := funAndParams[0].(string)
switch(fun){
case "DoVirtual":
fmt.Println("IM DoVirtual B implementation")
}
}}
return &B{&a}
}
func (this *A) Do() {
fmt.Println("IM DO")
this.virFunDispatcher("DoVirtual")
}
func (this *A) DoVirtual() {
fmt.Println("IM DoVirtual Default implementation")
}
func (this *B) DoVirtual() {
fmt.Println("IM DoVirtual B implementation")
}
func main() {
a := newA()
a.Do()
fmt.Println("----")
b := newB()
b.DoVirtual()
b.Do() // How to make here B.DoVirtual call
}https://stackoverflow.com/questions/36884609
复制相似问题