OC中类目,扩展,协议的介绍

类目
类目(categroy):指向已知的类增加新的方法,不会破坏封装性。
1;在类目中定义的方法会成为原始类的一部分,调用与其它方法相同。
2:在父类中添加类目子类会继承该类目中的方法,但在子类中添加类目父类无法拥有这些方法。
类目(categroy)定义
命名规则: 类名+扩张方法 类目不继承于父类,但接口与定义类很相似,用一个括号表示用途
类目(categroy)的应用
1:对类进行扩展。
2:作为子类的替换,可以通过类目向已有类中添加方法,与定义子类一样。
3:把类中的方法归类,可以更好地管理和维护类。

类目的局限性
1:无法向类中添加实例变量,如果需要添加实例变量则只能在子类中添加。
2:如果在类目中重写父类方法,可能导致super消息的断裂,因为在类目中的方法优先级高于父类。
#import<Foundation/Foundation.h>
@interfaceNSMutableArray(sort)
-(void)pawpawSort;

@end


#import"NSMutableArray+sort.h"
@implementationNSMutableArray(sort)
-(void)pawpawSort{
for(inti=0;i<self.count;i++){
for(intj=0;j<self.count-i-1;j++){
if([self[j]intValue]>[self[j+1]intValue]){
[selfexchangeObjectAtIndex:jwithObjectAtIndex:j+1];
}
}
}
}
@end


然后在appdelegate.h中调用#import"NSMutableArray+sort.h"然后

NSMutableArray*array=[NSMutableArrayarrayWithObjects:@"4432",@"234",@"3432",@"321",nil];
[arraypawpawSort];
NSLog(@"%@",array);
个人心得:语法说不能给类添加属性,要知道属性就是方法,类目就是添加类方法的,所以个人认为是可以添加属性的~

延展
延展(Extension)在本类里声明私有方法
1:延展定义的方法是在implemetation中。
2:声明的方法是私有方法。

3:延展中声明的方法可以不实现。

#import"Host.h"
//延展的声明
@interfaceHost()
-(BOOL)hasHouse;
@end
@implementationHost

ios6中的用法就是以上的不要~

个人心得:可以在其他类中强制使用延展,但是系统会提示没有定义!延展是一种特殊的类目


协议
协议(protocol):声明一套方法,但是让别的类实现。协议没有父类也不能定义实例变量。
作用:
1:需要有别的类实现的方法
2:声明为之类的借口
3:两个类之间的通信

协议(protocol)的特点
1:协议声明的方法可以被任何类实现。
2:协议不是类,是定义了可以被其他类实现的接口。
3:如果在某个类中实现了协议中的一个方法,那么可以说这个类实现了该协议。

@required和@optional
required是必须要实现的方法,默认情况下是required

optional时可选择实现的方法。


#import<Foundation/Foundation.h>
@protocolClassProtocol<NSObject>
@required
-(void)notSheep;
-(void)notGetOffShoes;
@optional
-(void)notChat;
@end



#import<Foundation/Foundation.h>
#import"ClassProtocol.h"
@interfaceStudent:NSObject< ClassProtocol >{
NSString*_name;
}
@property(nonatomic,retain)NSString*name;
-(id)initWithName:(NSString*)aName;
-(void)leranClass:(NSString*)aClass;

@end


#import"Student.h"
@implementationStudent
@synthesizename=_name;
-(id)initWithName:(NSString*)aName{
self=[superinit];
if(self){
self.name=aName;
}
returnself;
}

-(void)leranClass:(NSString*)aClass{
NSLog(@"%@islearning%@",self.name,aClass);
}
-(void)notSheep{
NSLog(@"%@cannotsleep!!",self.name);
}

-(void)notGetOffShoes{
NSLog(@"%@cannotgetoffshoes",self.name);
}
-(void)dealloc{
[_namerelease];
[superdealloc];
}
@end

代理模式

即本身不做实际的事情,而要求其他人去做

例如要卖房子,不是直接销售,二十让中介去帮自己销售

中介就是我们的代理

//定义协议

#import<Foundation/Foundation.h>
@protocolRentDelegate<NSObject>
@required
-(void)renter:(id)aRenter
money:(int)aMoney
salar:(NSString*)aSalar;
@end



#import<Foundation/Foundation.h>
#import"RentDelegate.h"
#import"Host.h"
@interfaceHost:NSObject{
NSString*_name;
NSString*_address;
id<RentDelegate>_delegate;
}
@property(nonatomic,assign)id<RentDelegate>delegate;
@property(nonatomic,retain)NSString*name;
@property(nonatomic,retain)NSString*address;
-(id)initWithName:(NSString*)aName
Address:(NSString*)aAddress;
-(void)rentHouse:(NSString*)salarName;
@end

#import"Host.h"
@implementationHost
@synthesizename=_name,address=_address,delegate=_delegate;
-(id)initWithName:(NSString*)aNameAddress:(NSString*)aAddress{
self=[superinit];
if(self){
self.name=aName;
self.address=aAddress;
}
returnself;
}
-(void)rentHouse:(NSString*)salarName{
[self.delegaterenter:self.namemoney:100000salar:salarName];
}
-(void)dealloc{
[_namerelease];
[_addressrelease];
[superdealloc];
}
@end

#import<Foundation/Foundation.h>
#import"RentDelegate.h"
@interfaceSalar:NSObject<RentDelegate>{
NSString*_name;
}
@property(nonatomic,retain)NSString*name;
-(id)initWithName:(NSString*)aName;
@end


#import"Salar.h"

@implementationSalar
@synthesizename=_name;
-(id)initWithName:(NSString*)aName{
self=[superinit];
if(self){
self.name=aName;
}
returnself;
}
-(void)renter:(id)aRentermoney:(int)aMoneysalar:(NSString*)aSalar{
NSLog(@"%@代理%@:%d出租房子!",aSalar,aRenter,aMoney);
NSLog(@"房子出租成功!");
}
-(void)dealloc{
[_namerelease];
[superdealloc];
}
@end


//添加在AppDelegate.m

Salar*s=nil;
s=[[Salaralloc]initWithName:@"21世纪"];
Host*host;
host=[[Hostalloc]initWithName:@"wangzhibao"Address:@"eeeeeee"];
host.delegate=s;
[hostrentHouse:s.name];
[hostrelease];
[srelease];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值