在复合中 我认为比较重要多就是description;以下为详细讲解。
自定义对象描述(description)
1: 复合都是对象与对象的组合,为了能区分开各个对象,所以我们会需 要自定义描述(description)
2: 当使用NSLog()输出对象时,会输出该对象的描述。
3: 即NSLog给对象发送了一个description消息,然后对象的 description方法生成一个NSString并将其返回输出。
也就是组合,那也就是一个对象不是一个简单的个体,他都是很多对象在一起组合的,就比如说房子这个类,它是由窗户,地板,门,墙,--组合成的,然后大脑又有很多组合起来的,所以说万物皆对象,就是这么个道理,现在时节就是面向对象的世界,我们每天都要面对很多对象,用很多对象!
Window.h
#import <Foundation/Foundation.h>
@interface Window : NSObject
-(NSString*)win;
Window.m
#import "Window.h"
@implementation Window
-(NSString *)win{
return @"窗子";
}
Door.h
#import <Foundation/Foundation.h>
@interface Door : NSObject
-(NSString *)door;
@end
Door.m
#import "Door.h"
@implementation Door
-(NSString *)door{
return @"门";
}
@end
Floor.h
#import <Foundation/Foundation.h>
@interface Floor : NSObject
-(NSString*)floor;
@end
Floor.m
#import "Floor.h"
@implementation Floor
-(NSString *)floor{
return @"地板";
}
@end
House.h
#import <Foundation/Foundation.h>
#import "Window.h"
#import "Door.h"
#import "Floor.h"
@interface House : NSObject
{
Window *_window;
Door *_door;
Floor *_floor;
}
-(void)setWindow:(Window *)windows;
-(void)setDoor:(Door *)doors;
-(void)setFloor:(Floor *)floors;
-(void)setHome;
@end
House.m
#import "House.h"
@implementation House
-(void)setWindow:(Window *)windows
{
_window = windows;
}
-(void)setDoor:(Door *)doors
{
_door = doors;
}
-(void)setFloor:(Floor *)floors
{
_floor = floors;
}
-(void)setHome
{
NSLog(@"这里有一个家,家的装饰都有:%@,%@和%@",[_window win],[_door door],[_floor floor]);
}
@end
main.m
#import <Foundation/Foundation.h>
#import "Car.h"
#import "Engine.h"
#import "Type.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Car *newCar = [[Car alloc]init];
Engine *newEngine = [[Engine alloc]init];
Type *newType = [[Type alloc]init];
[newCar setEngine:newEngine];
[newCar setType:newType];
[newCar setCar];
}
return 0;
}