标签:
一切皆有可能
//
// main.m
#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
Student *stu=[[Student alloc]init];
[stu finishTeask];
[stu dontlate];
[stu wearNeat];//可选
}
return 0;
}
在协议中个默认的方法都是必须实现的
协议有两个关键字:@required和@optional
@required表示必须实现的方法
而@optional表示可选实现的方法
//
// Scsy.h
#import <Foundation/Foundation.h>
//@protocol Scsy协议<父类>
@protocol Scsy <NSObject>
@required//必须实现
//声明
//完成作业
-(void)finishTeask;
//不迟到
-(void)dontlate;
@optional//可选
//衣着整洁
-(void)wearNeat;
//
// Student.h
#import <Foundation/Foundation.h>
#import "Scsy.h"
@interface Student : NSObject<Scsy>
@end
//
// Student.m
#import "Student.h"
@implementation Student
-(void)finishTeask{
NSLog(@"完成作业");
}
-(void)dontlate{
NSLog(@"不能迟到");
}
-(void)wearNeat{
NSLog(@"衣着整洁");
}
@end
标签:
原文地址:http://www.cnblogs.com/wyhwyh2114/p/4927721.html