Go语言的接口编程模型与面向对象编程(OOP)相比如何?

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

How Go's interface programming model compares to OOP?

问题

我已经阅读了大部分Go教程,但我仍然不清楚Go的接口编程模型与面向对象编程(OOP)有何区别。

有人能解释一下我如何开始“以Go的方式思考”吗?

我对如何定义接口,然后基于接口创建对象感到困惑。

在编译时,Go是否会隐式地为您创建一个具体实现?

英文:

I have read through most of the Go tour tutorial, but I'm still unclear on how Go's interface programming model compares with OOP?

Can someone explain how I can start 'thinking in Go'?

I'm confused how you can define an interface, and then create objects based on the interface?

Does Go implicitly create an concrete implementation for you during compile time?

答案1

得分: 2

OOP通常试图解决的问题之一是多态性,或者说两个不同的类具有相同行为的实例的能力。通常在OOP中,这是通过使用继承来实现的。一个基类定义了一个最小的接口,其他类可以扩展这个接口。基类的所有子类都可以被用作基类。

Go语言通过使用接口来实现相同的功能,而不是继承。接口是一种“描述”行为的方式。在Go语言中,由各个类型来满足这个描述,通过实现接口中描述的每个方法。如果一个类型实现了接口中描述的所有方法,那么它自动满足该接口,并且可以被编译器自动转换为该接口。

英文:

One of the problems OOP usually tries to solve is Polymorphism or the the ability for two different classes to have instances that behave the same. Usually in OOP this is accomplished by using inheritance. A Base class defines a minimal interface that other classes extend. All of the subclasses of Base class can be used as a Base class.

Go does the same thing not by inheritance but using interfaces. An interface is a "description" of behaviour. It is up to the individual Types in Go to satisfy this description by implementing each of the methods described in the interface. If a Type does implement all the methods described in the interface then it automatically satisfies the interface and can be cast to that interface automatically by the compiler.

答案2

得分: 2

Go的接口系统类似于结构类型。考虑以下Python代码片段:

def foo(bar):
   bar.baz(5)

在这个代码片段中,我们不知道bar的具体类型,但我们可以说它必须有一个接受单个int参数的baz方法。请注意,当我们编写bar的类时,我们不必声明我们正在实现这个“接口”(具有接受整数的baz方法)。我们只需编写一个baz方法,在调用时使用单个int时能正确工作,并且我们可以将一个实例传递给foo方法。

Go的工作方式类似,但所有内容都在编译时进行检查。在Python中,如果我们向foo方法传递一个没有baz方法的类的实例,我们会得到一个运行时异常。在Go中,我们将定义一个具有baz方法的接口,并在foo的类型中声明它接受该接口的实例。现在,任何具有在单个int上具有baz方法的类型都满足foo所需的类型。

英文:

Go's interface system is akin to structural typing. Consider a snippet of python code:

def foo(bar):
   bar.baz(5)

In this snippet we don't know what concrete type bar is but we can say that it must have a baz method which accepts a single int argument. Note that when we're writing the class of bar we don't have to declare that we're implementing this 'interface' (having a baz method which takes an integer). We just write a baz method which works correctly when called with a single int and we can pass an instance to the foo method.

Go works in a similar manner but everything is checked at compile time. In Python if we pass our foo method an instance of a class which does not have a baz method we get a runtime exception. In Go we would define an interface with the baz method and state in the type of foo that it takes an instance of that interface. Now any type which has a baz method on a single int satisfies the type that foo requires.

答案3

得分: 0

传统(Java)面向对象编程是关于类层次结构的。你用类来建模问题,有些是抽象的,有些是最终的,还有接口。然后你提供实现。

Go语言让你可以反其道而行之:从具体类型开始,实现你的逻辑。如果出现或需要一个有用的抽象:将其封装到一个接口中,并重构你的代码以使用这个接口类型。

英文:

Traditional (Java) OO is about class hierarchies. You model your problem with classes, some abstract, some final and interfaces. Then you provide implementations.

Go lets you go the other way around: Start with concrete types and implement your logic. If a useful abstraction emerges or is required: Pack it into an interface and refactor your code to use this interface type.

huangapple
  • 本文由 发表于 2013年1月15日 00:01:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/14322015.html
匿名

发表评论

匿名网友

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

确定