Zope 组件架构组件是否需要声明它实现的接口?
注意:我是ZCA新手,所以代码可能不正确;不过,我对 ZCA 的工作方式有些熟悉。
举例来说:
class I1(Interface):
def c1():
pass
class U1(object):
implements(I1) #is this necessary?
def c1():
#do some things here
是否需要 implements(I1)
行,或者 ZCA 是否可以自行计算出 U1 实现了 I1(有点像 Go 中接口的工作方式)?
Note: I am new to ZCA, so the code may be incorrect; however, I am somewhat familiar with the way ZCA works.
Given for example:
class I1(Interface):
def c1():
pass
class U1(object):
implements(I1) #is this necessary?
def c1():
#do some things here
Is the implements(I1)
line needed, or can ZCA figure out on its own that U1 implements I1 (kind of like the way interfaces in Go work)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该组件不需要声明它。当组件在组件注册表中注册时,组件的接口非常重要,因为组件查找是在接口上完成的。
您可以通过多种方式告诉组件注册表组件实现哪个接口。一种方法(最常见的方法)是对组件本身使用 Implements() 调用。您还可以在注册组件时告诉组件注册表它实现了哪些接口。
还有一些用于检查组件是否实现特定接口的函数,主要用于测试。
所以,是的,ZCA 中的接口的工作方式与 Go 中的接口非常相似,但是添加了组件注册表(除非 Go 有该功能,但我错过了,我不是 Go 专家),您可以在其中快速查找基于组件的组件关于接口和名称。
The component does not need to state it. The interface for a component is mostly important when it is registered in the component registry, as the component lookup is done on the interfaces.
You can tell the component registry which interface a component implements in several ways. One way, the most common one, is to use the implements() call on the component itself. You can also tell the component registry which interfaces it implements when registering the component.
There are also functions for checking if a component implements a specific interface, mostly used in tests.
So yes, interfaces in ZCA works much as how they do in Go, but with the addition of a component registry (unless Go has that to but I missed it, I'm not a Go expert) where you can quickly look up components based on interfaces and names.