做兼职的网站有哪些工作,建设网站的新闻,中国知名十大室内设计公司排名,wordpress ediclot1、类别(category)和扩展
类别:OC动态特征许使用类别添加新的方法#xff0c;不需要创建子类
扩展#xff1a;和类别相似#xff0c;扩展相对于匿名类别 2、使用格式 类别#xff1a;interface 已有类(类别名)…end implmentaion 已有类(类别名)…end 扩展#xff1a;im…1、类别(category)和扩展
类别:OC动态特征许使用类别添加新的方法不需要创建子类
扩展和类别相似扩展相对于匿名类别 2、使用格式
类别interface 已有类(类别名)…end implmentaion 已有类(类别名)…end 扩展imterface 也有类()
{实例变量
}
//方法...end 3、哪些功能
1)、给一个现成的类增加类别然后实现其方法
2)、模块化设计
3)、调用私有方法没有在接口部分定义而是在类实现部分定义的方法相对于私有方法不许调用通过定义向前引用实现对私有方法调用 4、测试类别Demo
NSNumerfk.h#ifndef NSNumber_fk_h
#define NSNumber_fk_h
#import Foundation/Foundation.h
interface NSNumber(fk)
-(NSNumber *)add:(double)num2;
-(NSNumber *)sub:(double)num2;
end#endif /* NSNumber_fk_h */ NSNumerfk.m
#import Foundation/Foundation.h
#import NSNumberfk.himplementation NSNumber(fk)
-(NSNumber *)add:(double)num2
{return [NSNumber numberWithDouble:([self doubleValue] num2)];
}
-(NSNumber *)sub:(double)num2
{return [NSNumber numberWithDouble:([self doubleValue] - num2)];
}
end MyApple.h
#import Foundation/Foundation.h#ifndef MyApple_h
#define MyApple_h
interface MyApple : NSObject
property (nonatomic, assign) double weight;
property (nonatomic, copy) NSString *color;
//java方法构造方法一般没有返回oc这里需要返回id,记住
-(id)initWithColor:(NSString *)color weight:(double)weight;
end MyApple.m
#import MyApple.h
//#import MyApplefk.h#import Foundation/Foundation.h
implementation MyApple
synthesize color _color;
synthesize weight _weight;
-(id)initWithColor:(NSString *)color weight:(double)weight
{if (self [super init]){self.color color;self.weight weight;}return self;
}-(NSString *) description
{return [NSString stringWithFormat:MyApple[color%, weight%g], self.color, self.weight];
}
-(void)test
{NSLog(this is test method);
}
endMyApplefk.h
#import MyApple.h#ifndef MyApple_fk_h
#define MyApple_fk_h
interface MyApple(fk)
-(void)test;
end
#endif /* MyApple_fk_h */main.h #import MyApple.h
#import NSNumberfk.h
#import MyApplefk.hint main(int argc, char * argv[]) {autoreleasepool {NSNumber *num [NSNumber numberWithDouble:3.5];NSLog(3.5 - 2 is %, [num sub:2]);NSLog(3.5 2 is %, [num add:2]);MyApple *apple [[MyApple alloc] initWithColor:red weight:5.6];NSLog(%, apple);//No visible interface for MyApple declares the selector test[apple test];}
} 5、运行类别结果 3.5 - 2 is 1.5
3.5 2 is 5.5
MyApple[colorred, weight5.6]
this is test method 6、测试扩展的Demo Car.h #ifndef Car_h
#define Car_h#import Foundation/Foundation.h
interface Car : NSObject
property (nonatomic, copy) NSString *brand;
property (nonatomic, copy) NSString *model;
-(void)drive;
end
#endif /* Car_h */ Cardrive.h #import Car.h
#ifndef Car_drive_h
#define Car_drive_hinterface Car()
property (nonatomic, copy) NSString *color;
-(void)drive:(NSString *)owner;
end#endif /* Car_drive_h */ Car.m #import Foundation/Foundation.h
#import Cardrive.himplementation Car
synthesize brand;
synthesize model;
synthesize color;-(void)drive
{NSLog(%汽车正在路上奔驰,self);
}
-(void)drive:(NSString *)owner
{NSLog(%正驾驶%汽车在路上奔驰, owner, self);
}
-(NSString *)description
{return [NSString stringWithFormat:Car [brand%, model%, color%], self.brand, self.model, self.color];
}
endmain.m #import Cardrive.h
int main(int argc, char * argv[]) {autoreleasepool {Car *car [Car new];car.brand 奔驰;car.model S300;car.color red;[car drive];[car drive:chenyu];}
} 7、运行扩展的结果 Car [brand奔驰, modelS300, colorred]汽车正在路上奔驰
chenyu正驾驶Car [brand奔驰, modelS300, colorred]汽车在路上奔驰