在Go语言中,方法参数的继承替代方式

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

Inheritance substitute for method parameters in Go

问题

我理解到Go语言没有继承的概念,而是使用组合。以下是你的示例代码的翻译:

type Car struct {
    XMLName xml.Name `xml:"response"`
    // 从XML文件中获取的一些属性
}

type Audi struct {
    Car
    // 从XML文件中获取的更多属性
}

func UnmarshalFromXML(url string, car *Car) { /* 从XML文件中填充属性 */ }

你实际想要的是:

var audi Audi
UnmarshalFromXML("someURL", &audi)

但是这样是行不通的,因为Go语言中没有继承的概念。但是你需要使用结构体来使用XML文件的解组功能。

那么该怎么办呢?

英文:

I understood that Go has no inheritance and works with composition.

Here is my small example:

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 */ }

What I actually want:

var audi Audi
UnmarshalFromXML("someURL", &audi)

But this does not work, because there is no inheritance. But I need structs to use the unmarshal functionality for XML files.

So what to do?

答案1

得分: 3

go中,继承与其他语言非常不同。当一个类型从父类型扩展时,这两个类型仍然保持不同。在你的情况下,解决方案是创建一个接口,并将接口作为参数传递给函数。请看下面的示例:

package main

import (
	"fmt"
)

// 创建一个接口
type Vehicle interface {
	Move()
}

type Car struct {
	Color string
	Brand string
}

// Car有一个Move()方法,自动实现了Vehicle接口
func (c *Car) Move() {
	fmt.Print("Car is moving")
}

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

type Audi struct {
	EngineType string
	Car
}

// 传递一个接口
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中的面向对象编程有很好的解释:http://spf13.com/post/is-go-object-oriented/

英文:

Inheritance in go very different from another languages. When a type extended from a parent type, the two types remain distinct. The solution in your case is to create an interface, and pass interface as parameter in the function. Take a look to the example below:

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)
}

https://play.golang.org/p/ZO4P_3fjmz

This article have a great explanation of OOP in go: http://spf13.com/post/is-go-object-oriented/

答案2

得分: 1

你可以尝试使用接口代替。

type Car interface {
   theCar()
}

type CarBase struct {
}

func (_ CarBase) theCar() {
}
  
type Audi struct {
  CarBase
}

func UnmarshalFromXML(url string, car Car) 
英文:

You could try using interfaces instead

    type Car interface {
       theCar()
    }

    type CarBase struct {
    }

    func (_ CarBase) theCar() {
    }
      
    type Audi struct {
      CarBase
    }

    func UnmarshalFromXML(url string, car Car) 

huangapple
  • 本文由 发表于 2017年7月27日 16:17:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/45345029.html
匿名

发表评论

匿名网友

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

确定