Go接口用于约束集合和继承。

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

Go interface for set of constraints and inheritance

问题

我还在努力理解Golang的接口。请纠正我并帮助我理解。

Frances Campoy解释说,interface是一组约束条件。

所以在我的情况下,假设我有一个Store接口,它需要与约束一起使用,就像Go中的Interface排序一样。

type Store interface {
    Earning() int
    Expense() int
}

那么如果我想将这个接口约束实现到其他包中,比如StoreAStoreB,我需要做什么呢?当我尝试以下代码时,我希望得到一条错误信息:

aa := StoreC{}
aa.Add("AAA")
// 错误提示:`StoreC`未实现`Add`方法,受`Store`接口约束...一些内容

那么如果我想在其他Store中强制执行这个约束,我该怎么做?我需要像继承一样做些什么吗?

type StoreC interface {
    Store
}

换句话说,我想知道如何将package sort的三个方法的接口约束强制应用到Go中的每个sort操作。它是如何对任何其他可能的sort用法强制执行约束的?

谢谢!

英文:

I am still trying to understand Golang interface. Please correct me and help me understand.

Frances Campoy explains, interface is a set of contraints.

So in my case, let's say I have one Store interface that is to be interfaced with contrains, like sort Interface in Go.

type Store interface {
	Earning() int
	Expense() int
}

Then what do I have to do if I want to implement this interface constraint to other packages like StoreA, StoreB? I want to get a message like when I try:

aa := StoreC{}
aa.Add("AAA")
// error saying `StoreC` does not implement method `Add`, constrained by interface `Store`... something

So what do I do if I want to enforce this contraint in other Stores? Do I need to do something like inheritance?

type StoreC interface {
    Store
}

In other words, I wonder how package sort's interface of 3 methods can be enforced to every sort operations in Go. How does it enforce the contraints to any other possible sort uses in Go?

Thanks!

答案1

得分: 1

接口对于接口的使用者来说非常有用。通过查看接口提供的方法,您可以知道可以安全使用哪些方法。

如果您有一个接口:

type MyInterface interface {
   Frob(int)
   Flanged() bool
}

您知道任何函数 func foo(thing MyInterface) 都可以安全地调用 thing.Frob(..)thing.Flanged()

任何实现了 func (r a_type) Frob(int)func (r a_type) Flanged() bool 的类型都将满足该接口。编译器将在编译时进行此检查。

定义一个方法大致如下:

func (receiver a_type) methodname (可能还有更多参数) 可能的返回类型 {...}

接收器的类型是定义该方法的类型。方法的名称位于接收器类型之间,然后是(可能为空的)参数列表。

英文:

An interface is mostly useful for a user of the interface. By looking at what methods the interface provides, you know what methods you can safely use.

If you have an interface:

type MyInterface interface {
   Frob(int)
   Flanged() bool
}

You know that any function func foo(thing MyInterface) can safely call thing.Frob(..) and thing.Flanged() safely.

Any type that implements both a func (r a_type) Frob(int) and func (r a_type) Flanged() bool will satisfy the interface. The compiler will do this check at compile-time.

Defining a method looks (roughly) like this:

func (reciever a_type) methodname (possibly more args here) possibly_a_return_type {...}

The receiver's type is the type for which the method is defined. The name of the method comes between and then there's the (possibly empty) argument list.

huangapple
  • 本文由 发表于 2014年11月24日 01:02:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/27091688.html
匿名

发表评论

匿名网友

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

确定