Java接口和Objective-C协议之间的区别? [英] Differences between Java interfaces and Objective-C protocols?

查看:153
本文介绍了Java接口和Objective-C协议之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认识Java,现在我正在学习Objective-C。 Java接口和Objective-C协议之间究竟有什么区别?

I know Java, and now I'm learning Objective-C. What exactly are the differences between Java interfaces and Objective-C protocols?

推荐答案

首先,一点关于该主题的历史观点,来自Java的创建者之一。接下来,维基百科有一个适度有用的关于Objective-C协议的部分。特别要明白,Objective-C支持正式协议(使用 @protocol 关键字显式声明,相当于Java接口)和非正式协议(只是一个或多个类实现的方法,可以通过反射发现)。

First off, a little historical perspective on the topic, from one of the creators of Java. Next, Wikipedia has a moderately helpful section on Objective-C protocols. In particular, understand that Objective-C supports both formal protocols (which are explicitly declared with the @protocol keyword, the equivalent of a Java interface) and informal protocols (just one or more methods implemented by a class, which can be discovered via reflection).

如果采用正式协议(实现接口的Objective-C术语)编译器将为未实现的方法发出警告,就像您在Java中所期望的那样。 与Java(如 skaffman 提到的)不同,如果Objective-C类实现了正式协议中包含的方法,则称它符合该协议,即使它的接口没有明确采用它。您可以在代码中测试协议一致性(使用 - conformsToProtocol:),如下所示:

If you adopt a formal protocol (Objective-C terminology for "implement an interface") the compiler will emit warnings for unimplemented methods, just as you would expect in Java. Unlike Java (as skaffman mentioned), if an Objective-C class implements the methods contained in a formal protocol, it is said to "conform" to that protocol, even if its interface doesn't explicitly adopt it. You can test protocol conformance in code (using -conformsToProtocol:) like this:

if ([myObject conformsToProtocol:@protocol(MyProtocol)]) {
    ...
}

注意:Apple的文档声明:

NOTE: Apple's documentation states:


此方法确定一致性如上所示,在头文件中的正式声明的基础上。它不会检查协议中声明的方法是否实际实现 - 这是程序员的责任。

"This method determines conformance solely on the basis of the formal declarations in header files, as illustrated above. It doesn’t check to see whether the methods declared in the protocol are actually implemented—that’s the programmer’s responsibility."

目标-C 2.0(在OS X 10.5Leopard和iOS中),正式协议现在可以定义可选方法,并且只要它实现了所有必需的方法,类就符合协议。您可以使用 @required (默认)和 @optional 关键字来切换后面的方法声明是否必须可能实施以符合协议。(请参阅Apple的 Objective-C 2.0编程语言指南讨论可选协议m方法。)

As of Objective-C 2.0 (in OS X 10.5 "Leopard" and iOS), formal protocols can now define optional methods, and a class conforms to a protocol as long as it implements all the required methods. You can use the @required (default) and @optional keywords to toggle whether the method declarations that follow must or may be implemented to conform to the protocol. (See the section of Apple's Objective-C 2.0 Programming Language guide that discusses optional protocol methods.)

可选的协议方法为开发人员提供了很大的灵活性,特别是对于实现代理监听器即可。而不是扩展类似 MouseInputAdapter (这可能很烦人,因为Java也是单继承)或实现了许多无意义的空方法,你可以采用一个协议并只实现你关心的可选方法。使用此模式,调用者在调用之前检查方法是否已实现(使用 - respondsToSelector ),如下所示:

Optional protocol methods open up a lot of flexibility to developers, particularly for implementing delegates and listeners. Instead of extending something like a MouseInputAdapter (which can be annoying, since Java is also single-inheritance) or implementing a lot of pointless, empty methods, you can adopt a protocol and implement only the optional methods you care about. With this pattern, the caller checks whether the method is implemented before invoking it (using -respondsToSelector) like so:

if ([myObject respondsToSelector:@selector(fillArray:withObject:)]) {
    [myObject fillArray:anArray withObject:foo];
    ...
}

如果反射的开销成为问题,您可以随时缓存布尔结果以便重复使用,但要抵制过早优化的冲动。 : - )

If the overhead of reflection becomes a problem, you can always cache the boolean result for reuse, but resist the urge to optimize prematurely. :-)

这篇关于Java接口和Objective-C协议之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆