Oop Go中方法参数的继承替换

Oop Go中方法参数的继承替换,oop,inheritance,go,Oop,Inheritance,Go,我明白围棋没有继承性,只能用构图来创作 以下是我的小例子: type Car struct { XMLName xml.Name `xml:"response"` // some properties coming from a XML file } type Audi struct { Car // some more properties coming from a XML file } func UnmarshalFromXML(url string,

我明白围棋没有继承性,只能用构图来创作

以下是我的小例子:

type Car struct {
    XMLName xml.Name `xml:"response"`
    // some properties coming from a XML file
}

type Audi struct {
    Car
    // some more properties coming from a XML file
}

func UnmarshalFromXML(url string, car *Car) { /* fill the properties from the XML file */ }
我真正想要的是:

var audi Audi
UnmarshalFromXML("someURL", &audi)
但这不起作用,因为没有继承。但是我需要结构来使用XML文件的解组功能


那么该怎么办呢?

您可以尝试改用接口

    type Car interface {
       theCar()
    }

    type CarBase struct {
    }

    func (_ CarBase) theCar() {
    }

    type Audi struct {
      CarBase
    }

    func UnmarshalFromXML(url string, car Car) 

您可以尝试改用接口

    type Car interface {
       theCar()
    }

    type CarBase struct {
    }

    func (_ CarBase) theCar() {
    }

    type Audi struct {
      CarBase
    }

    func UnmarshalFromXML(url string, car Car) 
go中的继承与其他语言非常不同。当一个类型从父类型扩展时,这两种类型保持不同。本例中的解决方案是创建一个接口,并将接口作为参数传递到函数中。请看下面的示例:

package main

import (
    "fmt"
)

// creating an interface
type Vehicle interface {
    Move()
}

type Car struct {
    Color string
    Brand string
}

// Car has method Move(), automatically stated implement Vehicle interface
func (c *Car) Move() {
    fmt.Print("Car is moving")
}

func(c *Car) Honk() {
    fmt.Print("Teeeet... Teet")
}

type Audi struct {
    EngineType string
    Car
}

// passing an interface
func UnmarshalFromXML(url string, v Vehicle) { 
    fmt.Print(v)
    v.Honk() 

}


func main() {
    var car = Car{Color:"Green"}
    var audi = Audi{}
    audi.Car = car
    audi.Brand = "Audi"
    audi.EngineType = "EC"

    UnmarshalFromXML("someURL", &audi)
}
本文对go中的OOP进行了很好的解释:

go中的继承与其他语言有很大的不同。当一个类型从父类型扩展时,这两种类型保持不同。本例中的解决方案是创建一个接口,并将接口作为参数传递到函数中。请看下面的示例:

package main

import (
    "fmt"
)

// creating an interface
type Vehicle interface {
    Move()
}

type Car struct {
    Color string
    Brand string
}

// Car has method Move(), automatically stated implement Vehicle interface
func (c *Car) Move() {
    fmt.Print("Car is moving")
}

func(c *Car) Honk() {
    fmt.Print("Teeeet... Teet")
}

type Audi struct {
    EngineType string
    Car
}

// passing an interface
func UnmarshalFromXML(url string, v Vehicle) { 
    fmt.Print(v)
    v.Honk() 

}


func main() {
    var car = Car{Color:"Green"}
    var audi = Audi{}
    audi.Car = car
    audi.Brand = "Audi"
    audi.EngineType = "EC"

    UnmarshalFromXML("someURL", &audi)
}

这篇文章对go中的OOP有很好的解释:

顺便说一句,如果您在具体处理XML并使用编码/XML进行解组时遇到问题,那么您的函数签名应该类似于解组半romXmlURL字符串,car接口{}错误,如果您在具体处理XML并使用encoding/XML进行解组时遇到问题,那么您的函数签名应该类似于解组HalfRomXmlURL字符串,car接口{}错误。不幸的是,这不起作用,因为我们需要一些属性来解组XML。@MichaelDorner,是什么阻止您将属性添加到类中?据我所知,Go with类中没有继承。没有继承,但您可以将属性添加到基类或子类中,这样就可以了。我一直使用JSON。不幸的是,这不起作用,因为我们需要一些属性来进行XML解组。@MichaelDorner,是什么阻止您将属性添加到类中?据我所知,Go with类中没有继承。没有继承,但您可以将属性添加到基类或子类中,这样就可以了。我一直在用JSON做这件事。