23

Protocol例子

@protocol Printing-(void)print;@end ////  Book.h #import <Cocoa/Cocoa.h>#import "printing.h" @interface Book : NSObject <Printing> {	NSString* bookname;} @property (retain) NSString* bookname; -(id)initWithBookname:(NSString*)name;@end ////  Book.m #import "Book.h"  @implementation Book @synthesize bookname; -(void)print{	NSLog(@"%@", self.bookname);} -(id)initWithBookname:(NSString*)name{	self = [super init];	if(nil!=self){		self.bookname = name;	}	return self;} -(void)dealloc{	[bookname release];	[super dealloc];} @end //main.m #import <Foundation/Foundation.h>#import "Book.h" int main (int argc, const char * argv[]) {NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];	NSString* bookname = [[NSString alloc]initWithFormat:@"Twilight"];	Book* book = [[Book alloc]initWithBookname:bookname];	[bookname release]; 	if([book conformsToProtocol:@protocol(Printing)]){		[book print];	} 	[book release];    [pool drain];    return 0;}

当前没有评论!

第一个在本文留言。

发表评论

名字(必须)
邮箱(必须),(永不被公布)
网址(建议)

字体为 粗体 是必填项目,邮箱地址 永远不会 公布。

允许部分 HTML 代码:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
URLs(网站链接)必须完整有效 (比如: http://www.macgg.com),所有标签都必须完整的关闭。

超出部分系统将会自动分段及换行。

请保证评论内容是与日志或 Blog 内容相关的,灌水、攻击性或不恰当的评论 可能 会被编辑或删除。

    有问题,搜一搜

    最近文章

    热文月度排行

    功能