dongshaidu2456 2014-11-18 01:42
浏览 25
已采纳

如何在Go中进行简单继承

I'm a Python developer, trying to learn Go. Currently I'm trying to refactor my first small project, but I am not too sure how to share a method between structs.

Long story short, how would you do something like this Python code in Go?

class Super(object):

  def CommonMethod(self):
    print 'I am the common method.'


class One(Super):

  def MethodOne(self):
    self.CommonMethod()
    print 'I am method one.'


class Two(Super):

  def MethodTwo(self):
    self.CommonMethod()
    print 'I am method two.'

one = One()
one.MethodOne()

two = Two()
two.MethodTwo()
  • 写回答

3条回答 默认 最新

  • duanmajing9332 2014-11-18 02:19
    关注

    TL;DR

    In Go methods aren't just magically inherited with subclassing as you would do in other languages like Python or Java. You can define interfaces and using embedding but you'll have to implement the methods you need for each type. Of course you can just call the method of the embedded from the outer method, however be careful that any changes will occur to the inner object and not the outer one.

    From the docs:

    There's an important way in which embedding differs from subclassing. When we embed a type, the methods of that type become methods of the outer type, but when they are invoked the receiver of the method is the inner type, not the outer one. In our example, when the Read method of a bufio.ReadWriter is invoked, it has exactly the same effect as the forwarding method written out above; the receiver is the reader field of the ReadWriter, not the ReadWriter itself.

    Some more info

    Here's some references from the docs:

    http://golang.org/doc/faq#Is_Go_an_object-oriented_language

    Is Go an object-oriented language?

    Yes and no. Although Go has types and methods and allows an object-oriented style of programming, there is no type hierarchy. The concept of “interface” in Go provides a different approach that we believe is easy to use and in some ways more general. There are also ways to embed types in other types to provide something analogous—but not identical—to subclassing.

    So, you can have interfaces that define what should be implemented in a type but you'll have to implement those methods for each type.

    One convenience you have is Embedding:

    http://golang.org/doc/effective_go.html#embedding

    Go does not provide the typical, type-driven notion of subclassing, but it does have the ability to “borrow” pieces of an implementation by embedding types within a struct or interface.

    Interface embedding is very simple. We've mentioned the io.Reader and io.Writer interfaces before; here are their definitions.

    type Reader interface {
        Read(p []byte) (n int, err error)
    }
    
    type Writer interface {
        Write(p []byte) (n int, err error)
    } 
    

    The io package also exports several other interfaces that specify objects that can implement several such methods. For instance, there is io.ReadWriter, an interface containing both Read and Write. We could specify io.ReadWriter by listing the two methods explicitly, but it's easier and more evocative to embed the two interfaces to form the new one, like this:

    // ReadWriter is the interface that combines the Reader and Writer interfaces.
    type ReadWriter interface {
        Reader
        Writer
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了