组合和多重继承

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

Composition and multiple inheritance

问题

由于Go使用组合系统而不是(多重)继承,我只想知道这三个代码片段。Go说它们强制程序员使用组合。

A)应该是(几乎)正确的Go代码,
B)伪代码
C)伪代码

在我看来,这三个代码的结果总是相同的,除了B)和C)可以用于更多的东西,而A)则强制你坚持使用组合?

即使你假设B)不在类内部具有sort方法,而是像A)一样全局,也没有真正的区别。

A)Go代码:

interface Sort
    Len()
    Less(i, j int) bool
    Swap(i, j int)

func (qs *Sort) sort()
    doTheSorting

type MyData struct {
    var value int
}

func (s *MyData) Len() { ... }
func (s *MyData) Less(i, j int) bool { ... }
func (s *MyData) Swap(i, j int) { ... }

B)看起来像继承,但在编译器的工作方式下可以被视为嵌入。

class Sort

    public sort() { ... }

    abstract Len()
    abstract Less(i, j int) bool
    abstract Swap(i, j int)

C)

interface SortInterface
    void Len()
    bool Less(i, j int)
    void Swap(i, j int)

class Sort implements SortInterface
    public sort() { ... }

使用B和C:

class MyClass **embed** Sort

    int value

    void Len() { ... }
    bool Less(i, j int) { ... }
    void Swap(i, j int) { ... }
英文:

Since Go uses an composition system instead of (multiple) inheritance, I'm just wondering about these 3 code snippets. Go says they force the programmer to use composition.

A) should be (almost) correct Go-Code,
B) pseudo
C) pseudo

Imho the result will always be the same on all three codes, beside the fact, that B) and C) can be used for even more stuff and A) forces you to stick to composition?

Even if you assume B) to not have the sort-method inside of the class but - lets say global like A) doesn't make a real difference oO

A) Go code:

interface Sort
    Len()
    Less(i, j int) bool
    Swap(i, j int)

func (qs *Sort) sort()
    doTheSorting

type MyData struct {
    var value int
}

func (s *MyData) Len() { ... }
func (s *MyData) Less(i, j int) bool { ... }
func (s *MyData) Swap(i, j int) { ... }

B) Looks like Inheritance but can imho be seen as embedded, according to how the compiler works.

class Sort

    public sort() { ... }

    abstract Len()
    abstract Less(i, j int) bool
    abstract Swap(i, j int)

C)

interface SortInterface
    void Len()
    bool Less(i, j int)
    void Swap(i, j int)

class Sort implements SortInterface
    public sort() { ... }

Usage B and C:

class MyClass **embed** Sort

    int value

    void Len() { ... }
    bool Less(i, j int) { ... }
    void Swap(i, j int) { ... }

答案1

得分: 4

这不是Go语言的工作方式。这里有一个例子(从标准库中提取的),展示了一个可以排序的类型。

type IntSlice []int

func (p IntSlice) Len() int           { return len(p) }
func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p IntSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

这个实现了接口:

// 为了清晰起见,接口的名称已更改
type Sort interface {
    Len() int
    Less(i, j int) bool
    Swap(i, j int)
}

实现了Sort接口的类型不会得到一个新的方法。你不能像你的例子中那样给接口分配一个方法,比如func (qs *Sort) sort() {...}

然而,它可以被传递给期望类型为Sort的变量的函数和方法。因此,我可以调用sort.Sort(myIntSlice)来对其进行排序。

这里有一个接受任何实现了Sort接口的参数的示例函数:

func IsSorted(data Sort) bool {
    n := data.Len()
    for i := n - 1; i > 0; i-- {
        if data.Less(i, i-1) {
            return false
        }
    }
    return true
}

IsSorted中,函数不知道data的真实类型是什么。它可以是IntSlice或其他任何类型。它所知道的是,无论你给它什么参数,它都实现了Sort接口中的方法。

然而,我似乎无法理解你所问的问题。此外,伪代码很难理解。使用其他语言,比如Java,可能会更好一些。

英文:

No this is not how go works. Here is an example (pulled from the standard library) of a type that can be sorted.

type IntSlice []int

func (p IntSlice) Len() int           { return len(p) }
func (p IntSlice) Less(i, j int) bool { return p[i] &lt; p[j] }
func (p IntSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

This implements the interface:

// Name of interface changed for clarity
type Sort interface {
    Len() int
    Less(i, j int) bool
    Swap(i, j int)
}

A type which implement the Sort interface does not get a new method. You can not assign a method to an interface such as in your example func (qs *Sort) sort() {...}.

However, it is allowed to be passed to functions and methods expecting a variable of type Sort. Because of this, I am able to call sort.Sort(myIntSlice) and it will then be sorted.

Here is an example function which takes any parameter that implements the Sort interface:

func IsSorted(data Sort) bool {
    n := data.Len()
    for i := n - 1; i &gt; 0; i-- {
        if data.Less(i, i-1) {
            return false
        }
    }
    return true
}

In IsSorted, the function has no idea what the true type of data is. It could be IntSlice or anything else. What it does know is that whatever parameter you gave it implements the methods in the Sort interface.

I do not seem to be able to figure out the question you asked however. Also, psuedo code is very difficult to understand. Using another language such as java would have been better.

huangapple
  • 本文由 发表于 2012年6月22日 07:32:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/11148299.html
匿名

发表评论

匿名网友

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

确定