如何代表和界面可以互换使用? [英] How can a delegate & interface be used interchangeably?

查看:122
本文介绍了如何代表和界面可以互换使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用接口方法代替委托吗?怎么样?我发现搜索接口方法比使用委托更快。我将很感激一个简单的代码片段。

Can I use interface method instead of delegate? How? I found searching that interface method is faster than using delegates. I would appreciate a simple code snippet.

推荐答案

理论上,可以通过接口完成代理所做的一切(Java没有例如,代表包含一个方法。但是,它使代码更加冗长,并且几乎没有带来任何好处。然后,可以在没有类的情况下完成所有操作,并且可以更快地完成。这并不意味着你应该避免使用类。调用委托方法可能略微比调用接口方法慢。你不应该关心微观优化。使用符合您特定需求的任何东西。

In theory, it's possible to accomplish everything done by delegates with interfaces (Java has no delegates, for instance) containing a single method. However, it makes the code more verbose and adds little benefit. Then again, it's possible to do everything without classes and possibly do it faster. That doesn't mean you should avoid using classes. Calling a delegate method might be marginally slower than calling an interface method. You shouldn't care about the micro-optimization. Use whatever fits your specific need.

这篇关于如何代表和界面可以互换使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆