• objective-C 复合(组合)


      

    复合:将多个组件组合在一起,形成一个整体,然后使用整体来进行工作,这种组合行为称为复合
    复合后的整体功能更加强大
    编写一个房屋类,由门,窗户等组成,门和窗有自己的开关方法,然后将门和窗这些复合在一起称为房屋类
    调用房屋类的进出方法实际调用到门的开关
    调用房屋的换气方法,实际调用到窗户的开关
    // 由多个类的对象组成一个大类,就是一个组合的过程
    #import <Foundation/Foundation.h>
    #import "Widow.h"
    #import "Door.h"
    @interface House : NSObject
    
    @property (nonatomic, strong) Door *door;
    @property (nonatomic, strong) Widow *widow;
    -(void)inouthouse;
    -(void)exchangair;
    @end
    #import "House.h"
    
    @implementation House
    
    -(void)inouthouse{
        [_door open ];NSLog(@"进屋");
        [_door close];  NSLog(@"出屋");
    }
    -(void)exchangair{
         [_widow open];
        NSLog(@"开始换气");
        [_widow close];
        NSLog(@"换气结束");
    }
    @end
    
    
    #import <Foundation/Foundation.h>
    @interface Widow : NSObject -(void)open; 
    -(void)close; @end #import "Widow.h"

    @implementation Widow
    -(void)open{ NSLog(@"开窗"); }

    -(void)close{ NSLog(@"关窗"); } @end
    #import <Foundation/Foundation.h>
    
    @interface Door : NSObject
    -(void)open;
    -(void)close;
    @end
    
    #import "Door.h"
    
    @implementation Door
    -(void)open{
        NSLog(@"开门");
    }
    -(void)close{
        NSLog(@"关门");
    }
    @end
    #import <Foundation/Foundation.h>
    #import "House.h"
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            House *h=[[House alloc]init];
            h.door=[[Door alloc]init];
            h.widow=[[Widow alloc]init];
            
            [h inouthouse];
            [h exchangair];
         
        }
        return 0;
    }
  • 相关阅读:
    Web框架
    Java并发编程:Callable、Future和FutureTask
    5分钟了解Mockito
    用IntelliJ IDEA创建Gradle项目简单入门
    QrenCode : linux命令行下生成二维码图片
    git 撤销commit
    curl网站开发指南
    H2数据库攻略
    Guice入门
    linux debian 时间设置中无法选择“自动设定时间和日期”
  • 原文地址:https://www.cnblogs.com/wyhwyh2114/p/4945962.html
Copyright © 2020-2023  润新知