Function Over Loading in GO using interfaces

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

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  // &lt;====
	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 &quot;fmt&quot;

type interf interface {
	typeCheck()
}

type subtype struct{}

func (subtype) typeCheck() {
	fmt.Println(&quot;Hi,this is printed in Sub-type&quot;)
}

type maintype struct {
	subtype
}

func (maintype) typeCheck() {
	fmt.Println(&quot;Hi,this is printed in Main type&quot;)
}

func main() {
	var intr interf
	var maintp maintype
	intr = maintp
	// &quot;Hi,this is printed in Main type&quot;
	intr.typeCheck()
	// &quot;Hi,this is printed in Sub-type&quot;
	intr.(maintype).subtype.typeCheck()
}

Output:

Hi,this is printed in Main type
Hi,this is printed in Sub-type

huangapple
  • 本文由 发表于 2014年8月1日 16:16:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/25075797.html
匿名

发表评论

匿名网友

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

确定