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

我想知道 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

https://stackoverflow.com/questions/1679145/

相关文章:

iphone - 你需要在 GCD 的一个 block 中创建一个 NSAutoreleasePoo

ios - 是否可以调试 "Terminated due to memory error"?

iphone - 实现 copyWithZone : 时的最佳实践

iphone - iOS 7 UIRefreshControl tintColor 不适用于 beg

ios - NSPredicate 用于测试 NULL 和空白字符串

ios - 如何为 NSDate 添加一个月?

objective-c - 删除 NSString 的最后一个字符

objective-c - 在 Objective-C 中从字符串中删除空格

objective-c - 整数总是初始化为0吗?

ios - 在主线程上调用方法?