Objective-C 的类别功能属于哪个 OOPS 功能?
据我所知,Objective-C是一种面向对象的编程语言,而Categories是Objective-C提供的一个功能。
所以我想知道类别功能属于哪个OOP概念
- 抽象
- 多态
- 封装
- 继承等。
提前致谢。
姆鲁纳尔
As per my knowledge, Objective-C is an Object oriented programming languge and Categories is a feature provided by Objective-C.
So I would like to know that Category feature is coming under which OOPs concept
- Abstraction
- Polymorphism
- Encapsulation
- Inheritance, etc.
Thanks in advance.
Mrunal
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@Abizern的回答很好。我想补充一点,类别是动态调度的一种形式,特别是它们可以用来扩展现有的类而无需子类化。
也就是说,面向对象编程更多的是一种设计理念,而不是一组语言功能。有人可能会问“后缀增量对应什么 OOP 特性?”答案是“没有;这是一种语言功能”。类别主要不是用于实现 OOP 设计(尽管有时它们是用于实现 OOP 设计,如上所述)。它们最初的用途是分解大型实施文件。由于语言缺陷(缺少
@optional
),它们后来的用途是提供非正式协议。如今,它们主要用于沿着特定于平台的线路分割代码(NSString+UIStringDrawing
与NSString+AppKitAdditions
)。扩展与类别类似,同样主要是一种语言功能而不是 OOP 设计功能。它们在某种程度上促进了封装,但大多数是任意编译器要求在使用方法之前定义方法的副作用(我说“任意”是因为这与设计或开发人员的需求无关;它只是简化了编译器)。扩展不应与某些深层的 OOP 需求相混淆。
因此,使用类别在运行时附加附加功能是动态调度。除此之外,它只是一个用于一些非 OOP 事物的语言功能。
@Abizern's answer is good. I would add that categories are a form of dynamic dispatch, in particular that they can be used to extend existing classes without subclassing.
That said, Object Oriented Programming is more a design philosophy than a set of language features. One might ask "what OOP feature does postfix increment correspond to?" The answer is "none; it's a language feature." Categories are not primarily used to implement OOP design (though sometimes they are, as noted above). Their original use was to break up large implementation files. Their later use was to provide informal protocols due to a flaw in the language (lack of
@optional
). And today, they're primarily used to split code along platform-specific lines (NSString+UIStringDrawing
vsNSString+AppKitAdditions
).Extensions are similar to categories, and similarly are primarily a language feature rather than an OOP design feature. They facilitate encapsulation to some extent, but mostly are a side-effect of an arbitrary compiler requirement to define methods before they are used (I say "arbitrary" because this is not related to design or developer needs; it just simplifies the compiler). Extensions should not be confused with some deep OOP requirement.
So using categories to attach additional functionality at runtime is dynamic dispatch. Beyond that, it's just a language feature that's used for several non-OOP things.
根据 Buck 和 Yacktman 的 Cocoa 设计模式书。 类别本身就是一种模式,并且由 Objective-C 编程语言直接支持。
According to the Cocoa Design Patterns book by Buck and Yacktman. Category is a pattern in itself, and one that is supported by the Objective-C programming language directly.
当涉及学术 OOP 概念时,可能最接近封装。但这确实显示了 OOP 的学术定义与 OOP 设计模式的实际世界之间的差异。
Probably closest to Encapsulation, when it comes to academic OOP concepts. But this really shows the difference between academic definitions of OOP, and the practical world of OOP Design Patterns.