方法和协议有什么区别

发布于 2024-10-01 23:39:16 字数 54 浏览 0 评论 0原文

他们说协议是一种方法,但它与方法不同。那么确切地说,它的作用是什么?方法和协议之间有什么区别?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海未深 2024-10-08 23:39:16

协议绝对不是方法!协议是类在遵守该协议时实现的一组方法。它类似于 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

秋意浓 2024-10-08 23:39:16

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.

北城挽邺 2024-10-08 23:39:16

协议是类的对象可以实现的一组方法。

假设您有一个由方法 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文