duck java_Duck类型和(java)接口概念

如果你阅读了它的介绍(PEP 3119)背后的基本原理,你会很快意识到部分原因是为了“拯救约翰免于死亡”,或者换句话说,当你编程到一个接口时,所有接口方法都会在那里。来自链接的政治公众人物:ABCs are simply Python classes that

are added into an object's inheritance

tree to signal certain features of

that object to an external inspector.

Tests are done using isinstance(), and

the presence of a particular ABC means

that the test has passed. In addition,

the ABCs define a minimal set of

methods that establish the

characteristic behavior of the type.

Code that discriminates objects based

on their ABC type can trust that those

methods will always be present.

通常,您可以对自己的代码应用相同的模式。例如:您可以创建一个BasePlugin类,其中包含插件工作所需的所有方法,然后可以通过子类化创建几个不同的插件。根据每个插件是否必须或可以定义这些方法,您可以定义BasePlugin方法来静默传递(plugins可以定义这些方法)或引发异常(plugins必须定义这些方法/重写BasePlugin的方法)。在

编辑:在下面的评论中,有人建议我在回答中加入以下几点讨论:This kind of features - at least in python - are not implemented for the sake of the human programmer (python never silence an error, so there's already plenty of feedback there), but rather for the sake of python own introspection capability (thus making easier to write dynamic loading, metaprogramming code, etc...). In other words: I know John can't fly... but I want the python interpreter to know it too! :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值