英文:
Function Over Loading in GO using interfaces
问题
我有一个"主类型",其中嵌入了一个"子类型"。主类型和子类型都实现了一个接口。
当我将一个"主类型"变量赋值给接口类型变量,并使用该接口变量调用实现的方法时,它调用的是"主类型"的实现,而不是子类型的。我需要调用子类型的实现。
在Go语言中是否可能实现这个需求?我认为我的代码设计存在一些问题。这里我提供了一个示例代码来描述这个问题。
package main
import "fmt"
type interf interface {
typeCheck()
}
type maintype struct {
subtype
}
type subtype struct{}
func (maintype) typeCheck() {
fmt.Println("Hi, this is printed in Main type")
}
func (subtype) typeCheck() {
fmt.Println("Hi, this is printed in Sub-type")
}
func main() {
var intr interf
var maintp maintype
intr = maintp
intr.typeCheck() // 输出:"Hi, this is printed in Main type",我需要的是:"Hi, this is printed in Sub-type"
}
PlayGround: http://play.golang.org/p/ut5XPiED75
请帮忙看看。
英文:
I have a "main type" and a "sub-type" is embedded in that.Both main and sub implements an interface.
When I'm assigning a 'main type' variable to interface type variable and call implemented method using that interface variable,It calls "main type" implementation not sub type's.I need to call sub-types implementation.
Is it possible in GO ? I think my code design is having some problem. Here I'm giving a sample code to describe this problem.
package main
import "fmt"
type interf interface{
typeCheck()
}
type maintype struct{
subtype
}
type subtype struct {}
func (maintype) typeCheck () {
fmt.Println("Hi,this is printed in Main type")
}
func (subtype) typeCheck () {
fmt.Println("Hi,this is printed in Sub-type")
}
func main() {
var intr interf
var maintp maintype
intr = maintp
intr.typeCheck()//Out :"Hi,this is printed in Main type" I need "Hi,this is printed in Sub-type"
}
PlayGround : http://play.golang.org/p/ut5XPiED75
Kindly Help....
答案1
得分: 4
你需要明确地将嵌入的子类型分配给接口:
func main() {
var intr interf
var maintp maintype
intr = maintp.subtype // <====
intr.typeCheck()
}
输出(在 play.golang.org 上):
嗨,这是在子类型中打印的内容
文章 "Inheritance Semantics in Go" 详细介绍了为什么它不起作用,在 "Type Embedding and Implementation Inheritance" 部分中。它的解决方案在 "Type Substitution Through Interfaces" 中。
Go 通过使用接口来实现类型替换。接口允许我们定义抽象行为,并使不同的类型满足该行为。
然而,它仍然依赖于将正确的子类型赋给接口变量。
英文:
You need to explicitly assign to your interface the embedded subtype:
func main() {
var intr interf
var maintp maintype
intr = maintp.subtype // <====
intr.typeCheck()
}
output (<kbd>play.golang.org</kbd>):
Hi,this is printed in Sub-type
The article "Inheritance Semantics in Go" details why it doesn't work, in the "Type Embedding and Implementation Inheritance" section.
Its solution is in "Type Substitution Through Interfaces"
> Go accomplishes type substitution through the use of Interfaces.
Interfaces allow us to define an abstract behaviour and have different types satisfy that behaviour.
It still relies on affecting the right subtype to the interface variable though.
答案2
得分: 2
例如,
package main
import "fmt"
type interf interface {
typeCheck()
}
type subtype struct{}
func (subtype) typeCheck() {
fmt.Println("嗨,这是在子类型中打印的")
}
type maintype struct {
subtype
}
func (maintype) typeCheck() {
fmt.Println("嗨,这是在主类型中打印的")
}
func main() {
var intr interf
var maintp maintype
intr = maintp
// "嗨,这是在主类型中打印的"
intr.typeCheck()
// "嗨,这是在子类型中打印的"
intr.(maintype).subtype.typeCheck()
}
输出:
嗨,这是在主类型中打印的
嗨,这是在子类型中打印的
英文:
For example,
package main
import "fmt"
type interf interface {
typeCheck()
}
type subtype struct{}
func (subtype) typeCheck() {
fmt.Println("Hi,this is printed in Sub-type")
}
type maintype struct {
subtype
}
func (maintype) typeCheck() {
fmt.Println("Hi,this is printed in Main type")
}
func main() {
var intr interf
var maintp maintype
intr = maintp
// "Hi,this is printed in Main type"
intr.typeCheck()
// "Hi,this is printed in Sub-type"
intr.(maintype).subtype.typeCheck()
}
Output:
Hi,this is printed in Main type
Hi,this is printed in Sub-type
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论