OC中的复合
#import <Foundation/Foundation.h> #import "Car.h" int main(int argc, const char * argv[]) { @autoreleasepool { Car *ca =[Car new]; Engine *eng=[Engine new]; eng.pingpai=@"🌲.❤️.☁️.现代"; ca.engine=eng; for (int i=0; i<4; i++) { Tire *t=[Tire new]; [ca.tires addObject:t]; } [ca zuzhuang]; [ca run]; } return 0; }
#import <Foundation/Foundation.h> #import "Engine.h" #import "Tire.h" @interface Car : NSObject @property(strong,nonatomic)Engine *engine; @property(strong,nonatomic) NSMutableArray *tires; -(void)zuzhuang; -(void)run; @end
#import "Car.h" @implementation Car - (instancetype)init { self = [super init]; if (self) { self.tires=[NSMutableArray arrayWithCapacity:0]; } return self; } -(void)zuzhuang{ NSLog(@"开始组装"); for (int i=0;i<[self.tires count]; i++) { NSLog(@"安装 %d 个轮子",i+1); } // NSLog(@"%@",self.tires); NSLog(@"%@",self.engine); NSLog(@"组装完毕"); } -(void)run{ NSLog(@"跑........."); } @end
#import <Foundation/Foundation.h> @interface Engine : NSObject @property(strong,nonatomic) NSString *pingpai; @end
#import "Engine.h" @implementation Engine - (NSString *)description { return [NSString stringWithFormat:@"我是%@引擎",_pingpai]; } @end
#import <Foundation/Foundation.h> @interface Tire : NSObject @end
@implementation Tire - (NSString *)description { return [NSString stringWithFormat:@"轮子"]; } @end
2.电脑组装
#import <Foundation/Foundation.h> #import "Computer.h" int main(int argc, const char * argv[]) { @autoreleasepool { Computer *com=[Computer new]; Display *dis=[Display new]; com.display=dis; yingpan *yip=[yingpan new]; com.yp=yip; Mouse *m=[Mouse new]; com.mouse=m; KeyBpard *k=[KeyBpard new]; com.keybpard=k; [com zhuzhuang]; [com open]; [com music]; [com program]; [com close]; } return 0; }
#import <Foundation/Foundation.h> @interface Display : NSObject @end
#import "Display.h" @implementation Display - (NSString *)description { return [NSString stringWithFormat:@"显示器"]; } @end
#import <Foundation/Foundation.h> @interface KeyBpard : NSObject @end
#import "KeyBpard.h" @implementation KeyBpard - (NSString *)description { return [NSString stringWithFormat:@"键盘"]; } @end
#import <Foundation/Foundation.h> @interface Mouse : NSObject @end
#import <Foundation/Foundation.h> @interface Mouse : NSObject @end
#import <Foundation/Foundation.h> #import "yingpan.h" #import "Display.h" #import "KeyBpard.h" #import "Mouse.h" @interface Computer : NSObject @property(strong,nonatomic) yingpan *yp; @property(strong,nonatomic) Display *display; @property(strong,nonatomic) KeyBpard *keybpard; @property(strong,nonatomic) Mouse *mouse; -(void)zhuzhuang; -(void)open; -(void)close; -(void)music; -(void)program; @end
#import "Computer.h" @implementation Computer -(void) zhuzhuang{ NSLog(@"组装机器"); NSLog(@"%@",self.yp); NSLog(@"%@",self.mouse); NSLog(@"%@",self.keybpard); NSLog(@"%@",self.display); NSLog(@"组装完毕"); } -(void)open{ NSLog(@"开机"); } -(void)close{ NSLog(@"关机"); } -(void)music{ NSLog(@"听音乐"); } -(void)program{ NSLog(@"编程"); } @end
#import <Foundation/Foundation.h> @interface yingpan : NSObject @end
#import "yingpan.h" @implementation yingpan - (NSString *)description { return [NSString stringWithFormat:@"机箱"]; } @end