由于很长时间没写博客了,差点都荒废了。其实主要的原因是最近在做项目,没有太多的时间去写博客,不像以前
那样无忧无虑的学习技术了。不过虽然最近没怎么写博客,但是最近的水平进步还是不小的。由于对于java的体系学的
时间还可以了,有点厌烦了,再加上老师的推荐,所以最近准备转战Object-c的苹果开发,我记得讲java 的张龙老师也
是现在在做OC的开发,呵呵,程序员嘛,不能总在一棵树上面吊死,得多试几棵树才行,哈哈。反正我在大学的时间
也不是很多了,可能留给我的空闲的学习时间也不是很多了,那么现在只能说是去珍惜时光好好的去学习好最新的自己
想学习的技术了,也不枉费爸妈的一片苦心了,呵呵,好了,废话不多说,继续开始学习了,I am Back!!!
首先对于复合,就是将多个组建组合在一起配合使用,从而得到完整的作品的东西。在OC中,复合是通过包含作为
实例变量的对象指针来实现的。比如
@interface Car : NSObject
{
Engine *engine;
Tire *tires[4];
}
@end
每个对象都有一个description方法,这个方法就是使用NSString输出对象的时候所对应的字符串,你可以覆盖实现输出自己的东西。主要的代码如下
#import <Foundation/Foundation.h>
@interface Tire : NSObject
@end
@implementation Tire
- (NSString *) description{
return (@"i am a tire, i last a while");
}
@end
@interface Engine : NSObject
@end
@implementation Engine
- (NSString *)description{
return (@"i am a engine wuwuwuwu");
}
@end
@interface Car : NSObject
{
Engine *engine;
Tire *tires[4];
}
- (Engine *) engine;
- (void) setEngine: (Engine *) newEngine;
- (Tire *) tireAtindex : (int) index;
- (void) setTile: (Tire *) tile atindex: (int) index;
- (void) print;
@end
@implementation Car
- (Engine *)engine{
return engine;
}
- (void)setEngine:(Engine *)newEngine{
engine = newEngine;
}
- (void)setTile:(Tire *)tile atindex:(int)index{
if(index <0 || index> 3){
NSLog(@"bad index %d in setTire:atindex",index);
exit(1);
}
tires[index]=tile;
}
- (Tire *)tireAtindex:(int)index{
if(index < 0 || index > 3){
NSLog(@"bad index %d in tireAtIndex",index);
exit(1);
}
return tires[index];
}
- (void)print{
NSLog(@"%@", engine);
NSLog(@"%@", tires[0]);
NSLog(@"%@", tires[1]);
NSLog(@"%@", tires[2]);
NSLog(@"%@", tires[3]);
}
@end
@interface Slant6 : Engine
@end
@implementation Slant6
- (NSString *)description{
return (@"i am a slant-6 veoom");
}
@end
@interface AllWeatherRadial : Tire
@end
@implementation AllWeatherRadial
- (NSString *)description{
return (@"i am a tire for rain or shine");
}
@end
int main(int argc, const char *argv[]){
Car *car = [Car new];
Engine *engine = [Slant6 new];
[car setEngine:engine];
for(int i = 0; i < 4; i++){
Tire *tire = [AllWeatherRadial new];
[car setTile:tire atindex:i];
}
[car print];
return 0;
}
其实我对于这个两个参数的书写还不是太清楚怎么回事,而且对于文件的管理也不是很懂,呵呵,继续学习吧,现在又成为一个菜鸟了。