方法和协议有什么区别
他们说协议是一种方法,但它与方法不同。那么确切地说,它的作用是什么?方法和协议之间有什么区别?
They say that a protocol is a method, but it's different from a method. So exactly, what does it do and what is the difference between a method and a protocol?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
协议绝对不是方法!协议是类在遵守该协议时实现的一组方法。它类似于 Java 接口。请参阅此内容丰富的讨论:Objective-C 与 Java 接口
A protocol is most definitely NOT a method! A protocol is a set of methods that a class implements when conforming to that protocol. It is similar to a Java interface. See this informative discussion: Objective-C versus Java Interface
在Objective-C中,每个类都有一个接口、该类实现的方法定义列表以及实现,每个方法定义的来源。
协议是方法定义的列表。类可以通过实现协议定义的方法来支持协议。
In Objective-C each class has an interface, a list of method defenitions which that class implements, and an implementation, the source for each of those method defenitions.
A protocol is a list of method definitions. A class can support a protocol by implementing the methods it defines.
协议是类的对象可以实现的一组方法。
假设您有一个由方法 A 和方法 B 组成的协议,那么如果对象实现了方法 A 和方法 B,则该对象符合该协议。
协议也可能包含不需要实现的可选方法。
A protocol is a set of methods that objects of a class may implement.
Let's say you have a protocol that consists of a method A and a method B, then an object conforms to that protocol if it implements method A and method B.
It's also possible for a protocol to include optional methods which are not required to be implemented.