Go 语言中的多态性

问题描述 投票:0回答:5

我正在学习 go lang,我想知道是否有办法做这样的事情:

type Foo struct {
   ...
}

type Bar struct {
   Foo
   ...
}

func getFoo() Foo {
   return Bar{...}
}

在面向对象的语言中,这样的代码应该可以毫无问题地工作,但在 go 中它会抛出一个错误,说

getFoo()
必须返回 Foo 类的实例。

有没有一种方法可以实现类似于我在 Go 中描述的多态性?

oop go polymorphism
5个回答
21
投票

Go 不是典型的 OO 语言。此外,每种语言都有自己的做事方式。你可以使用接口和组合来实现你想要的,如下所示:

package main

import "fmt"

type Foo interface {
   printFoo()
}

type FooImpl struct {

}

type Bar struct {
   FooImpl
}

type Bar2 struct {
   FooImpl
}

func (f FooImpl)printFoo(){
    fmt.Println("Print Foo Impl")
}

func getFoo() Foo {
   return Bar{}
}

func main() {
    fmt.Println("Hello, playground")
    b := getFoo()
    b.printFoo()
}

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


6
投票

在Go中,多态性是通过实现接口来实现的。

type Being interface {
        somemethod()
}

type Foo struct {}

type Bar struct {
        Foo
}

type Baz struct {
        Foo
}

// `Bar` and `Baz` implement `Being`
func (b *Bar) somemethod() {}
func (b *Baz) somemethod() {}

func getAnyFoo(b *Being) Foo {
   return b.Foo
}

因此,任何东西都实现一个空接口。

type Foo struct {}

type Bar struct {
        Foo
}

// Get anything and extract its `Foo` if anything is a Bar
func getAnyFoo(i interface{}) Foo {
        // Normally this would need a type switch to check the type
        mybar := i.(Bar)
        return mybar.Foo
}

0
投票

如果您知道要使用的类型,则可以将它们放入数组列表中以实现一种多态实例化:

package main
import (
  "fmt"
  "encoding/json"
)

type  Hans struct{
      Miau string
    }

type  Keule struct {
  Wuff string
}

func (K Keule)ppp() {
  fmt.Printf(K.Wuff)
}

func (K Hans)ppp() {
  fmt.Printf(K.Miau)
}

func (K Keule)TypeInfo() int {
  return 0
}

func (K Hans)TypeInfo() int {
  return 1
}

type Mega interface {
  ppp()
  TypeInfo() int
}


var j_a = `{
  "Kein_Alter": "nix",
  "Miau": "lala",
  "class": 0
}`

var j_b = `{
  "Alter": "nix",
  "Wuff": "lolo",
  "Class": 1
}`

type Class struct {
  Class int
}

func (K *Class)ppp() {
  fmt.Printf("%d", K.Class)
}

func Unmarshal_K(b []byte) (Mega, error) {
  var k Keule
  err := json.Unmarshal([]byte(j_a), &k)
  return k, err
}

func Unmarshal_H(b []byte) (Mega, error) {
  var k Hans
  err := json.Unmarshal([]byte(j_a), &k)
  return k, err
}

var UList = []func(b []byte) (Mega, error) {Unmarshal_H, Unmarshal_K}

func main() {
  var mv Class
  err := json.Unmarshal([]byte(j_a), &mv)
  if err != nil {
    panic(err)
  }
  

  hiho, err := UList[mv.Class]([]byte(j_a))
  if err != nil {
    panic(err)
  }


  hiho.ppp()
}

0
投票

您可以通过以下方式使用它。如果您给打印函数一个人或秘密特工,它会理解它直接来自人机界面并运行其中的函数。

包主

import "fmt"

type person struct {
    firstName string
    lastName  string
    age       int
}

type secretAgent struct {
    person
    ltk bool
}

type human interface {
    info() string
}

func (p person) info() string {
    return fmt.Sprint("Name:", p.firstName, " Surname:", p.lastName, " Age:", p.age)
}

func (s secretAgent) info() string {
    return fmt.Sprint("Name:", s.firstName, " Surname:", s.lastName, " Age:", s.age, " Ltk:", s.ltk)
}

func print(h human) {
    switch h.(type) {
    case person:
        fmt.Println("person struct:")
        fmt.Println(h.info())
    case secretAgent:
        fmt.Println("secretAgent struct:")
        fmt.Println(h.info())
    }
}

func main() {

    p := person{
        firstName: "Khanbala",
        lastName:  "Reshidov",
        age:       22,
    }

    s := secretAgent{
        person: p,
        ltk:    true,
    }

    //info Method
    fmt.Println(p.info())
    fmt.Println(s.info())

    //polymorphism

    print(p)
    print(s)

    //type
    fmt.Printf("%T\n", p)
    fmt.Printf("%T\n", s)
}

0
投票
package main

import "fmt"

// declare interface type
type Dog interface {
    Bark()
}

type Dalmatian struct {
    DogType string
}

type Labrador struct {
    DogType string
}

// implement the interface for both structs
func (d Dalmatian) Bark() {
    fmt.Println("Dalmatian barking!!")
}

func (l Labrador) Bark() {
    fmt.Println("Labrador barking!!")
}

// takes a Dog interface as a parameter
func MakeDogBark(d Dog) {
    d.Bark()
}

func main() {
    // create two instances of different structs that implement the Dog interface
    d := Dalmatian{"Jack"}
    l := Labrador{"Max"}

    // pass them to the function that accepts the Dog interface
    MakeDogBark(d) // Dalmatian barking!!
    MakeDogBark(l) // Labrador barking!!
}
golang 中的

多态性允许不同类型的结构体(斑点狗和拉布拉多犬)实现相同的接口(Dog),并传递给可以调用它们的接口方法(Bark)的相同函数(MakeDogbark)。

© www.soinside.com 2019 - 2024. All rights reserved.