Ruby:扩展 Net::IMAP 以包含其他命令?
是否可以扩展基类以包含其他命令?例如,假设我有一个自定义 IMAP 服务器,支持名为“WUNTEE”的命令...我想使用私有 Net::IMAP::send_command 方法发送“WUNTEE”,但由于它是私有的,如何这可能吗?这可能吗?谢谢。
Is it possible to extend a base class to include additional commands? For example, lets say I have a custom IMAP server that supports a command called "WUNTEE"... I would like to use the private Net::IMAP::send_command method to send "WUNTEE", but since its private, how would that be possible? Is that even possible? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以根据需要对其进行子类化(这是完全可以接受的),也可以使用您想要的新方法修补基类。从设计的角度来看,子类是一种更简洁的方法,但您可能会发现其他一些相关类直接使用 Net::IMAP 并且让它们使用您的特定版本是棘手或不切实际的。
实际上,通过在程序执行过程中的任何时候重新定义您不喜欢的方法来修补您自己的实现是非常简单的。
You can either subclass it as required, which is perfectly acceptable, or patch the base class with the new methods you want. From a design standpoint, a subclass is a cleaner approach, but you may find that some of the other related classes use Net::IMAP directly and getting them to use your specific version is tricky or impractical.
It's actually pretty straight-forward to patch out any methods you don't like with your own implementation by simply re-defining them at any point in the execution of your program.