objective-c - @interface 和 @protocol 解释?

标签 objective-c

我想知道 Objective-C 中的@interface 是什么?它只是程序员想要声明变量、类名或方法名的地方吗……?我不确定它是否像 Java 中的接口(interface)。 还有关于 Objective-C 中的@protocol。看起来更像是Java中的接口(interface)。 谁能给我详细的解释。我真的很感激。

最佳答案

接口(interface)是定义类的属性和操作的地方。你也必须制定实现。

协议(protocol)就像java的接口(interface)。

例如

@protocol Printing
    -(void) print;
@end

可以实现

通过声明(在界面中混淆)

@interface Fraction: NSObject <Printing, NSCopying> {
//etc..

对于 java 开发人员来说,令人困惑的是花括号 {} 不是接口(interface)的结尾,例如

@interface Forwarder : Object
{
    id recipient; 
} //This is not the end of the interface - just the operations


- (id) recipient;
- (id) setRecipient:(id) _recipient; 
//these are attributes.

@end
//This is the end of the interface

关于objective-c - @interface 和 @protocol 解释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1679145/

相关文章:

iphone - 在 iOS 中多个按钮上发出声音并在其他人播放时停止

ios - 你能拥有多个 UnityAppController 的子类吗?

objective-c - 如何将 unsafemutablerawpointer 作为 C 指针传递给 Objective-C++?

objective-c - OpenGLES 添加投影

ios - textview 文本到数组中

ios - 检测何时停止在 UITextField 中输入

iphone - 响应 UIScrollView 内以编程方式生成的自定义 UIButton 的触摸

ios - 创建框架时在 Xcode 8 中找不到 filename.h 文件错误

ios - XCode 应用程序随机崩溃 - 发送到已释放实例的消息

iphone - UIView 删除自己?