Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

objective c - @interface and @protocol explanation?

I would like to know what the @interface in objective C is? is it just where the programmer want to declare the variables, class name or method names...? I am not sure whether it is like interface in Java. And about the @protocol in objective C as well. It seems like the interface in Java more. Could anyone give me detail explanation please. I truly appreciate it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

An interface is where you define the attributes and operations of class. You must set out the implementation too.

A protocol is like an interface for java.

e.g.

@protocol Printing
    -(void) print;
@end

can be implemented

by declaring (confusingly in the interface)

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

The confusing thing for java developers is that the curly braces {} are not the end of the interface e.g.

@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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...