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

 

转载于:https://www.cnblogs.com/qianLL/p/5121602.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值