我想知道目标C中的@interface是什么?它只是程序员想要声明变量,类名或方法名称的地方......?我不确定它是否像Java中的界面.关于目标C中的@protocol也是如此.它看起来像Java中的界面更多.请问有谁能给我详细解释.我真的很感激.
Joh*_*lan 87
您可以在界面中定义类的属性和操作.您也必须列出实施.
协议就像java的接口.
例如
@protocol Printing
-(void) print;
@end
Run Code Online (Sandbox Code Playgroud)
可以实施
通过声明(在界面中容易混淆)
@interface Fraction: NSObject <Printing, NSCopying> {
//etc..
Run Code Online (Sandbox Code Playgroud)
对于Java开发人员来说,令人困惑的是花括号{}不是接口的末端,例如
@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
Run Code Online (Sandbox Code Playgroud)
And*_*rsK 26
如果你看看这个可能很好+我觉得这很有帮助
来自文章:
@接口
#ifndef __FOO_H__
#define __FOO_H__
class Foo
{
...
};
Run Code Online (Sandbox Code Playgroud)
#include "Foo.h"
...
Run Code Online (Sandbox Code Playgroud)
@interface Foo : NSObject
{
...
}
@end
Run Code Online (Sandbox Code Playgroud)
#import "Foo.h"
@implementation Foo
...
@end
Run Code Online (Sandbox Code Playgroud)
@协议
struct MyInterface
{
void foo() = 0;
}
class A : MyInterface
{
public:
void override foo() { ... }
}
Run Code Online (Sandbox Code Playgroud)
@protocol MyInterface
-(void) foo;
@end
@interface Foo : NSObject <MyInterface>
{
-(void) foo {...}
...
}
@end
Run Code Online (Sandbox Code Playgroud)
zou*_*oul 14
将@interface在Objective-C无关的Java接口.它只是声明一个类的公共接口,它的公共API.(正如您已经观察到的那样,成员变量.)Java样式的接口在Objective-C中称为协议,并使用该@protocol指令声明.您应该阅读Apple 的Objective-C编程语言,这是一本很好的书 - 简短且易于访问.
| 归档时间: |
|
| 查看次数: |
54265 次 |
| 最近记录: |