Objective-C 裡的 Protocol 與 Java 的 interface 或是 C++ 的 purely virtual class 相同。
Printing.h
@protocol Printing
-(void) print;
@end
Fraction.h
#import <Foundation/NSObject.h>
#import "Printing.h"
@interface Fraction: NSObject <Printing, NSCopying> {
int numerator;
int denominator;
}
-(Fraction*) initWithNumerator: (int) n denominator: (int) d;
-(void) setNumerator: (int) d;
-(void) setDenominator: (int) d;
-(void) setNumerator: (int) n andDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end
Fraction.m
#import "Fraction.h"
#import <stdio.h>
@implementation Fraction
-(Fraction*) initWithNumerator: (int) n denominator: (int) d {
self = [super init];
if ( self ) {
[self setNumerator: n andDenominator: d];
}
return self;
}
-(void) print {
printf( "%i/%i", numerator, denominator );
}
-(void) setNumerator: (int) n {
numerator = n;
}
-(void) setDenominator: (int) d {
denominator = d;
}
-(void) setNumerator: (int) n andDenominator: (int) d {
numerator = n;
denominator = d;
}
-(int) denominator {
return denominator;
}
-(int) numerator {
return numerator;
}
-(Fraction*) copyWithZone: (NSZone*) zone {
return [[Fraction allocWithZone: zone] initWithNumerator: numerator
denominator: denominator];
}
@end
Complex.h
#import <Foundation/NSObject.h>
#import "Printing.h"
@interface Complex: NSObject <Printing> {
double real;
double imaginary;
}
-(Complex*) initWithReal: (double) r andImaginary: (double) i;
-(void) setReal: (double) r;
-(void) setImaginary: (double) i;
-(void) setReal: (double) r andImaginary: (double) i;
-(double) real;
-(double) imaginary;
@end
Complex.m
#import "Complex.h"
#import <stdio.h>
@implementation Complex
-(Complex*) initWithReal: (double) r andImaginary: (double) i {
self = [super init];
if ( self ) {
[self setReal: r andImaginary: i];
}
return self;
}
-(void) setReal: (double) r {
real = r;
}
-(void) setImaginary: (double) i {
imaginary = i;
}
-(void) setReal: (double) r andImaginary: (double) i {
real = r;
imaginary = i;
}
-(double) real {
return real;
}
-(double) imaginary {
return imaginary;
}
-(void) print {
printf( "%_f + %_fi", real, imaginary );
}
@end
main.m
#import <stdio.h>
#import "Fraction.h"
#import "Complex.h"
int main( int argc, const char *argv[] ) {
// create a new instance
Fraction *frac = [[Fraction alloc] initWithNumerator: 3 denominator: 10];
Complex *comp = [[Complex alloc] initWithReal: 5 andImaginary: 15];
id <Printing> printable;
id <NSCopying, Printing> copyPrintable;
// print it
printable = frac;
printf( "The fraction is: " );
[printable print];
printf( "/n" );
// print complex
printable = comp;
printf( "The complex number is: " );
[printable print];
printf( "/n" );
// this compiles because Fraction comforms to both Printing and NSCopyable
copyPrintable = frac;
// this doesn't compile because Complex only conforms to Printing
//copyPrintable = comp;
// test conformance
// true
if ( [frac conformsToProtocol: @protocol( NSCopying )] == YES ) {
printf( "Fraction conforms to NSCopying/n" );
}
// false
if ( [comp conformsToProtocol: @protocol( NSCopying )] == YES ) {
printf( "Complex conforms to NSCopying/n" );
}
// free memory
[frac release];
[comp release];
return 0;
}
output
The fraction is: 3/10
The complex number is: 5.000000 + 15.000000i
Fraction conforms to NSCopying
protocol 的宣告十分簡單,基本上就是
@protocol ProtocolName
(methods youmust implement)
@end。
要遵從(conform)某個 protocol,將要遵從的 protocols 放在 <> 裡面,並以逗點
分隔。如:@interface SomeClass <Protocol1, Protocol2, Protocol3>
分享到:
相关推荐
这本书对于想快速掌握Objective‐C的人来说是一本相当不错的教材。
Objective‐C是开发Mac软件的主要编程语言。如果你了解一些面向对象的基本概念和 C语言,那么会对你学习Objective‐C有很多帮助,如果你不了解C,那么建议你先读一下 C 指南。
Objective-C基础教程.pdf(Learn Objective-C on the Mac中文版) 中文扫描版 + 英文电子书 + 源码 该资源在Mac下用BetterZIP压缩,共三个分卷,其他分卷在我的资源中找。 更新: Windows下使用7zip对part1解压...
learn Objective-c on the mac 的配套代码!
Learn Objective-C for Java Developers Learn Objective-C for Java Developers will guide experienced Java developers into the world of Objective-C. It will show them how to take their existing language...
Scott Knaster-Learn Objective–C on the Mac,学IOS的那本经典初级教程!!!
Learn Objective-C for Java Developers,为Java开发者准备的OC学习手册,高清英文。
Objective-C基础教程源代码 Learn objective-C on the Mac
如果你想迅速掌握Objective-C,并有c++或Java的基础,该文档绝对是你的首选。该文档共13页,全是Objective-C知识的精华,没有在此啰嗦与其他语言相同的部分。
elcome to Learn Objective- C on the Mac! This book is designed to teach you the basics of the Objective- C language. Objective- C is a superset of C and is the language used by many (if not most) ...
Learn Objective-C(zh)(v2) ios开发文档。
Learn Objective-C on the Mac.pdf
learn objective-c for java developers
关于Objective-C 编程语言的学习著作,Objective-C基础教程英文版第二版.
Learn Objective-C(中文版)(v2).pdfLearn Objective-C(中文版)(v2).pdfLearn Objective-C(中文版)(v2).pdfLearn Objective-C(中文版)(v2).pdf
Objective-C基础教程.pdf(Learn Objective-C on the Mac中文版) 中文扫描版 + 英文电子书 + 源码 该资源在Mac下用BetterZIP压缩,共三个分卷,其他分卷在我的资源中找。 更新: Windows下使用7zip对part1解压...
Objective‐C是开发Mac软件的主要编程语言。如果你了解一些面向对象的基本概念和C语言,那么会对你学习Objective‐C有很多帮助,如果你不了解C,那么建议你先读一下C 指南。
中文第二一版和英文第二版 是学习OC最好的书籍 推荐看英文的 因为以后的文档也是英文的 早晚得看
附录中还介绍了如何从其他语言过渡到Objective-C。 英文介绍:Take your coding skills to the next level with this extensive guide to Objective–C, the native programming language for developing ...