//1.類目 Category
//命名 : @interface 要擴展的類名 (類目名) ~~ @end
//特點 : 1.不可定義成員變量,而且一般不定義屬性
@interface NSString (SayBaby)
-(void)sayHello;
-(NSString *)reverse;
-(NSString *)substringFromIndex:(NSUInteger)from;
@end
--------------------
到NSString+SayBaby.m中去實現方法
#import "NSString+SayBaby.h"
@implementation NSString (SayBaby)
-(void)dealloc{
[super dealloc];
}
-(id)init{
self = [super init];
if (self) {
}
return self;
}
-(NSString *)substringFromIndex:(NSUInteger)from{
NSLog(@"%s",__func__);
return @"ok";
}
-(void)sayHello{
NSLog(@"%s",__func__);//打印出方法名
NSLog(@"self = %@",self);
}
//2.延展 Extension
//命名 : 寫在.m文件裏 @interface 類名 () ~~ @end
//特點 : 1.添加類的私有屬性和方法;
#import "Gril.h"
//延展(Extension):添加私有方法與成員變量,屬性
//延展的括號中沒有內容,如果添加內容,即類目
@interface Gril ()
{
NSInteger _age;
}
@property(nonatomic,assign)NSInteger age;
-(void)doNotAskAge;
@end
@implementation Gril
-(void)dealloc{
[_name release];
[_sex release];
[super dealloc];
}
-(id)init{
self = [super init];
if (self) {
self.age = 18;
[self doNotAskAge];
}
return self;
}
-(void)doNotAskAge{
NSLog(@"不要問");
}
@end
#import <Foundation/Foundation.h>
@interface Gril : NSObject
@property(nonatomic,retain)NSString *name;
@property(nonatomic,retain)NSString *sex;
@end
____________________________________________________________//3.協議 Protocol:實現類與類之間的通信
//命名 : @protocol 協議名 <NSObject> ~~ @end
// @required 不寫是默認的,必須實現 @optional 可實現可不實現
//特點 : 1.只聲明,不實現
// 2.爲了解決多繼承問題
// 3.@property (nonatomic, assign) id<協議名> delegate;
// 爲啥用assign 爲了防止循環擁有釋放不了