类与协议之间有什么区别

发布于 2024-12-21 03:31:26 字数 96 浏览 1 评论 0原文

我正在阅读文档,因为我即将实现一个协议而不是一个类(我以前从未做过的事情),并且我很好奇两者之间的区别。

有人可以用通俗易懂的话举个例子吗?

谢谢

I'm going through the docs because I am about to implement a protocol instead of a class (something I've never done before), and I'm curious as to the difference between the two.

Can someone give an example in plain words?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

深陷 2024-12-28 03:31:26

充当基于该类的特定实现创建一个或多个对象的蓝图。
一个很好的类比是切黄油饼干的形式。表单的属性(形状、大小、高度)定义了您可以用它剪切的cookie。您只有一种形式(类),但您可以用它创建许多 cookie(该类的实例,即对象)。所有 cookie 均基于该特定形式。
类似地,作为该类实例的所有对象的属性都是相同的。

类= 数据方法(特殊函数),全部巧妙地捆绑在一起。

类定义了,它们的内部内容(数据)是什么+它们能做什么工作(方法)
内容基于保存各种数字类型、字符串、常量和其他更复杂的内容+方法的变量,这些方法是(在执行时)对各种数据执行一些计算操作的代码块。

类中定义的所有方法都有其
定义 - 定义方法的名称 + 方法接收哪些数据(如果有)进行处理以及方法吐出哪些数据(如果有)供其他人处理。类中定义的所有方法也有实现 - 提供处理的实际代码 - 它是方法的内部工作。内部有处理数据的代码,并且能够向其他方法请求数据子处理数据。所以类是编程中非常高贵的类型。

如果你理解了上面的内容,你就会明白什么是协议。

协议是一组一个或多个方法声明,并且该集合具有名称并代表协议。我说声明,是因为这些方法一起由特定协议定义,没有定义任何实现代码。。唯一存在的是他们的名字。
看看上面 - 在类中,您始终不仅定义了类具有哪些方法,还定义了该工作将如何完成。但协议中的方法没有任何实现。

让我们再次用现实生活来类比,这会有所帮助。如果您来我家住一周,您将需要遵守我的 TidyUp 协议。 TidyUp协议定义了三种方法——每天洗碗、打扫房间、通风新鲜空气。这三种方法,我定义它们......是你会做的事情。但我绝对不关心实现应该是什么样子,我只是名义上定义方法。您将实现它们,即您定义该工作的细节(那些方法)将是什么样子。我只是说,遵守我的协议并按照您认为合适的方式实施。

Finale – 您可以声明一些课程。您还可以单独声明一个协议。然后您可以声明,这个类除了它自己的方法之外,还将采用或遵守该协议,即。该类将实现协议的方法。

A class serves as a blueprint for creating one or more objects based on specific implementation of that class.
A good analogy is a form for cutting out butter-cookies. The form‘s attributes (shape, size, height) define the cookies that you can cut out with it. You have only one form (class) but you can create many cookies (instances of that class, ie. objects) with it. All cookies are based on that particular form.
Similarily all objects that are instances of that class are identical in their attributes.

Classes = data and methods (special functions), all sophistically bundled together.

Classes define, what their inner content (data) is + what kind of work (methods) they can do.
The content is based on variables that hold various number types, strings, constants, and other more sophisiticated content + methods which are chunks of code that (when executed) perform some computational operations with various data.

All methods defined in class have their
Definition - that defines the name of the method + what (if any) data the methods takes in for processing and what (if any) data the methods spits out for processing by someone else. All methods defined in class also have Implementation – the actual code that provides the processing – it is the innerworkings of methods.. inside there is code that processes the data and also that is able to ask other methods for subprocessing data. So the class is a very noble type in programming.

If you understand the above, you will understand what a protocol is.

A protocol is a set of one or more method declarations and that set has a name and represents a protocol. I say declarations, because the methods that together are defined by a particular protocol, do not have any implementation code defined.. The only thing that exist is their names declared.
Look above - in class, you have always defined not only what methods the class has, but also how that work will be done. But methods in protocol do not have any implementation.

Lets have a real life analogy again, it helps. If you come to my house to live here for a week, you will need to adhere to my TidyUp protocol. The TidyUp protocol defines three methods - wash the dishes every day, clean the room, and ventilate fresh air. These three methods, I define them..are something you will do. But I absolutely do not care, how the implementation should look like, I just nominaly define the methods. You will implement them, ie.you define how the details of that work (those methods) will look like. I just say, adhere to my protocol and implement it as you see fit.

Finale – You can declare some class. You can separately also declare a protocol. And you can then declare, that this class, in addition to its own methods, will adopt or adhere to that protocol, ie. the class wil implement the protocol’s methods.

薄荷→糖丶微凉 2024-12-28 03:31:26

来自 Objective-C 编程语言简单地解释了协议的用途:

协议声明可以由任何类实现的方法。
协议至少在三种情况下很有用:

  • 声明其他人应该实现的方法
  • 声明对象的接口,同时隐藏其类
  • 捕获不具有层级关系的类之间的相似性

因此,协议声明方法,但不提供实现。采用协议的类应该实现该协议的方法。

委托是说明协议为何有用的一个很好的例子。例如,考虑 UITableViewDataSource 协议。任何类都可以采用该协议,并且任何这样做的类都可以用作表的数据源。表视图不关心什么类型的对象作为其数据源;它只关心充当数据源的对象实现一组特定的方法。您可以为此使用继承,但是所有数据源对象都必须从公共基类(比 NSObject 更具体)派生。相反,使用该协议让表能够调用 -tableView:willBeginEditingRowAtIndexPath:-tableView:heightForRowAtIndexPath: 等方法,而无需了解有关数据源的任何其他信息。

The plain words from The Objective-C Programming Language explain the purpose of protocols simply:

Protocols declare methods that can be implemented by any class.
Protocols are useful in at least three situations:

  • To declare methods that others are expected to implement
  • To declare the interface to an object while concealing its class
  • To capture similarities among classes that are not hierarchically related

So, protocols declare methods, but don't provide the implementation. A class that adopts a protocol is expected to implement the protocol's methods.

Delegation is a good example of why a protocol is useful. Consider, for example, the UITableViewDataSource protocol. Any class can adopt that protocol, and any class that does so can be used as the data source for a table. A table view doesn't care what kind of object is acting as its data source; it only cares that the object acting as data source implements a particular set of methods. You could use inheritance for this, but then all data source objects would have to be derived from a common base class (more specific than NSObject). Using the protocol instead lets the table count on being able to call methods like -tableView:willBeginEditingRowAtIndexPath: and -tableView:heightForRowAtIndexPath: without needing to know anything else about the data source.

强辩 2024-12-28 03:31:26

协议很像 Java 和其他语言中的接口。将其视为描述其他类同意实现的接口的契约。它可以定义实现类将实现的必需和可选方法的列表。与类不同,它不提供自己的这些方法的实现。

A protocol is a lot like an interface in Java and other languages. Think of it as a contract that describes the interface other classes agree to implement. It can define a list of required and optional methods that an implementing class will implement. Unlike a class, it does not provide its own implementations of those methods.

紫﹏色ふ单纯 2024-12-28 03:31:26

类和协议之间的主要区别在于编写协议对于实现委托方法很有用。

在示例中,我们有类 A 和类 B,并且我们想要从类 B 中调用类 A 中的方法。

您可以在本文中阅读一个非常有价值的示例

http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html

阅读代码胜过一千字;-)

这在我第一次使用时帮助了我不得不使用它们

the main difference between classes and protocols is that writing protocols is useful to implement delegate methods.

in example we've got class A and class B and we want to call a method in class A from the class B.

you can read a very valuable example of that in this article

http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html

reading code is worth a thousand words ;-)

that helped me out the first time I had to use'em

待天淡蓝洁白时 2024-12-28 03:31:26

与其他语言相比,差异稍小。接口(相当于 Java/C++ 类)定义对象的数据布局,并且可以定义其方法的某些子集(当然包括定义整个集合的可能性)。协议仅定义方法的子集,没有数据定义。

重要的是,一个接口只能从另一个接口继承(当然,另一个接口可以从一个接口继承,而另一个接口又又从另一个接口继承,而另一个接口又又继承...),但一个接口可以实现任意数量的协议。因此,两个没有共同继承的不同接口(除 NSObject 之外)都可以实现相同的协议,从而“证明”它们提供相同的功能。 (尽管使用 Objective-C,您可以通过一些技巧来调用未在接口声明或协议中外部声明的接口的方法,因此协议在某种程度上只是“语法糖”或类似的东西。)

Somewhat less difference than in other languages. An interface (equivalent to a Java/C++ class) defines the data layout of objects and may define some subset of their methods (including the possibility of defining the entire set, of course). A protocol defines a subset of methods only, with no data definition.

Of significance is that a interface can inherit from only one other interface (which can, of course, inherit from an interface which inherits from an interface which inherits ...), but an interface can implement any number of protocols. So two distinct interfaces with no common inheritance (other than NSObject) can both implement the same protocol and thus "certify" that they provide the same functions. (Though with Objective-C you can, with a few tricks, call methods of an interface that aren't externally declared in either the interface declaration or a protocol, so protocols are to a degree just "syntactic sugar" or some such.)

忘羡 2024-12-28 03:31:26

协议定义了类可以做什么,就像 Java 或 C# 中的接口一样,

类是完成该工作的实际实现。

够简单吗? :)

Protocol defines what a class could do, like a Interface in Java or c#

A class is the actual implementation that does the job.

Simple enough? :)

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