Objective-c - 类别和协议的目的

发布于 2024-11-16 04:56:56 字数 290 浏览 4 评论 0原文

我一直在阅读 Objective-c 协议和类别,但两者对我来说似乎毫无意义。它们都用于以某种有趣的方式向程序添加内容,而不仅仅是手动添加。

我可能会确信该协议的目的是让您可以检查 id 的类型,但仅此而已。我认为它们没有其他真正的用途。

至于类别,我真的不明白为什么你不将它们添加到代码中。为什么需要在要实现的类别中手动指定方法,而不是正常执行?您是否想创建一个“子类”但稍作修改?为什么不直接创建一个子类呢?

我真的不明白其中任何一个的目的,我希望有人能告诉我它们的真正用途=/

提前致谢, 基督教

I've been reading up on Objective-c Protocols and Categories and both seem rather pointless to me. They're both used for adding things to the program in some funny way instead of just manually adding it.

I might be convinced to see the purpose of the protocol being so that you can check the type of id's, but that's really about it. I see no other real use for them.

As for categories, I really don't see why you wouldn't just add them to the code. Why do you need to manually specify methods in a category that you're then gonna implement as opposed to just doing it normally? Is it that you might wanna make a "subclass" but with slight modifications? Why not just make a subclass?

I really don't see the purpose of either of these, I hope someone can tell me their real use =/

Thanks in advance,
Christian

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

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

发布评论

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

评论(5

她比我温柔 2024-11-23 04:56:56

类别是为您无法获取源代码的类添加方法,例如所有 Apple 类(以 NS、CG、CA 等开头的类),而无需对它们进行子类化。

协议的目的是定义遵守该协议的类必须实现的方法。在 Java 中,这些称为接口。目的是整理非兄弟类(同一超类的子类)之间的相似性。假设您有一个 Chair 类和一个 Petrol 类。它们没有太多共同点,只是它们都遵循 flammable 协议,这要求它们具有某些方法,例如 specienceEnergyflamingPoint

现在,您的 Fire 类可以有一个方法 addFlammableMaterial:(id)material

协议通常用于声明某些类的实例可以作为某些其他实例的委托。您可以通过声明视图控制器符合 UITableViewDataSource 协议来声明视图控制器充当 UITableView 的数据源,这意味着您的 viewController 保证它实现所需的方法的协议,并且 tableView 可以安全地休息,因为它可以信任 VC 作为其数据源。

Categories are to add methods to classes whose source is unavailable to you, such as all the Apple classes (those starting with NS, CG, CA, etc.), without needing to subclass them.

The purpose of protocols is to define methods that classes adhering to that protocol have to implement. In Java, those are called interfaces. The purpose is to codify similarities between classes that are not siblings (subclasses of the same superclass). Suppose you have a class Chair and a class Petrol. These don't have a lot in common, except that they both adhere to the flammable protocol, which requires them to have certain methods, such as specificEnergy and flamingPoint.

Now your Fire class can have a method addFlammableMaterial:(id <flammable>)material.

Protocols are often used to declare that instances of certain classes can be delegates for certain other instances. You can declare your view controller to act as the data source to a UITableView by declaring it to conform to the UITableViewDataSource protocol, which means your viewController guarantees that it implements the required methods of that protocol, and the tableView can rest safely because it can trust the VC to be its data source.

戏剧牡丹亭 2024-11-23 04:56:56

类别是一种向现有类的所有实例添加新方法而无需修改类本身的方法。

对于类别,请阅读已经讨论过的SO帖子,

继承和继承之间有什么区别Objective-C 中的类别

协议与类别

协议经常用于指定委托对象的接口。

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

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

了解协议的更多用法。

A category is a way of adding new methods to all instances of an existing class without modifying the class itself.

For category read the already discussed SO post,

What is the difference between inheritance and Categories in Objective-C

protocol-versus-category

Protocols are used frequently to specify the interface for delegate objects.

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

read more uses of protocols.

独享拥抱 2024-11-23 04:56:56

协议是将代码契约应用于类的一种方式,与 Java 和 C# 中接口的工作方式非常相似。

类别对于扩展无法直接修改的类非常有用,它们与 C# 中的扩展方法非常相似。

Protocols are a way of applying a code contract to a class, in much the same way that interfaces work in Java and C#.

Categories are useful to extend classes that you cannot directly modify, they are very similar to Extension methods in C#.

若无相欠,怎会相见 2024-11-23 04:56:56

协议的工作方式类似于 Java 中的接口。您可以使用一些方法定义协议,然后在许多类中实现它。这些类中的每一个都可以具有协议提供的方法的不同实现,但接口是相同的。例如:您想计算不同类型员工的工资,例如。 FullTimeEmployeeContractEmployee。您可以将 calculateSalary 方法定义放在协议中,并在这两个类中提供不同的实现,例如。一种是返还固定工资,另一种是根据工作时间返还工资。然后,您可以将每个类实例转换为协议类型并调用给定的方法接收适当的结果。

如果您想扩展一个不属于您的类,例如内置类,例如,类别会很有帮助。 NSString。您可以添加像 reverse 这样的方法来获取反转的字符串。为什么不创建一个子类?因为您可以在修改后的类上调用自定义方法,而无需强制转换对您的类型的引用。

Protocols work like interfaces in Java. You can define protocol with some methods and then implement it in many classes. Each of these classes could have different implementation of methods provided by protocol, but the interface would be the same. For example: you want to calculate salary of different types of employees, ex. FullTimeEmployee and ContractEmployee. You can put calculateSalary method definition in protocol and provide different implementations in these two classes, ex. one would return fixed salary while the other would return salary based on worked hours. Then you can cast each class instance to protocol type and invoke given method receiving appropriate results.

Categories are helpful if you want to extend a class which doesn't belong to you, like in case of built-in classes, ex. NSString. You can add a method like reverse to get reversed string. Why not to make a subclass? Because you can call your custom method on modified class without casting reference to your type.

恋竹姑娘 2024-11-23 04:56:56

类别可用于向现有类添加功能,例如所有 NS...类。您不能直接向其中添加代码。

协议用于指定当您调用协议中的方法时某个类实际上执行某些操作。

Categories can be used to add functionality to existing classes, such as all the NS... classes. You can not add code to these directly.

Protocols are used to specify that a certain class actually does something, when you call a method in the protocol.

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