doujia5863 2012-06-21 23:32
浏览 53
已采纳

组成和多重继承

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条回答 默认 最新

  • du42561 2012-06-22 00:25
    关注

    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] < 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 > 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。