Objective-C:dealloc、组合与聚合

1.dealloc方法

1.1 用于释放malloc的空间  (会自动调用)

.h文件

#import <Foundation/Foundation.h>

@interface CZExample : NSObject

@end

.m文件

#import "CZExample.h"

@implementation CZExample
-(instancetype)init
{
    if (self = [super init])
    {
        NSLog(@"对象被创建了");
    }
    return self;
}
-(void)dealloc
{
    NSLog(@"对象被销毁了");
}
@end

main函数

int main(int argc, const char * argv[]) {
    @autoreleasepool
    {
        CZExample *e = [[CZExample alloc] init];
        NSLog(@"------");
    }
    NSLog(@"++++++");
    return 0;
}

运行结果

2016-08-25 08:40:52.116 day16_01[649:21125] 对象被创建了
2016-08-25 08:40:52.116 day16_01[649:21125] ------
2016-08-25 08:40:52.117 day16_01[649:21125] 对象被销毁了
2016-08-25 08:40:52.117 day16_01[649:21125] ++++++
Program ended with exit code: 0

2. 组合与聚合

2.1  是指两个(以上)类之间的一种关系
2.2  是(一个)整体类与(多个)部分类之间的关系
2.3  组合是两个类的强关系,及同生共死。下面是组合的例子

建一个Button类
.h文件

#import <Foundation/Foundation.h>

@interface CZButton : NSObject

@end

.m文件

#import "CZButton.h"

@implementation CZButton
-(instancetype)init
{
    if (self = [super init])
    {
        NSLog(@"按钮被创建了");
    }
    return self;
}
-(void)dealloc
{
    NSLog(@"按钮被销毁了");
}
@end

建一个Edit类
.h文件

#import <Foundation/Foundation.h>

@interface CZEdit : NSObject

@end

.m文件

#import "CZEdit.h"

@implementation CZEdit
-(instancetype)init
{
    if (self = [super init])
    {
        NSLog(@"编辑框被创建了");
    }
    return self;
}
-(void)dealloc
{
    NSLog(@"编辑框被销毁了");
}
@end

建一个Window类
.h文件

#import <Foundation/Foundation.h>
#import "CZButton.h"
#import "CZEdit.h"

@interface CZWindow : NSObject
{
    CZButton *_button;//部分类CZButton只能作为整体类CZWindow的成员变量,不能用property定义成属性
    CZEdit *_edit;
}
@end

.m文件

#import "CZWindow.h"

@implementation CZWindow
-(instancetype)init
{
    if (self = [super init])
    {
        NSLog(@"窗口被创建了");
        _button = [[CZButton alloc] init];
        _edit = [[CZEdit alloc] init];
    }
    return self;
}
-(void)dealloc
{
    NSLog(@"窗口被销毁了");
}
@end

main函数

#import <Foundation/Foundation.h>
#import "CZWindow.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        CZWindow *win = [[CZWindow alloc] init];
        NSLog(@"------");
    }
    NSLog(@"++++++");
    return 0;
}

运行结果

2016-08-25 08:54:30.091 day16_03[706:27193] 窗口被创建了
2016-08-25 08:54:30.092 day16_03[706:27193] 按钮被创建了
2016-08-25 08:54:30.092 day16_03[706:27193] 编辑框被创建了
2016-08-25 08:54:30.092 day16_03[706:27193] ------
2016-08-25 08:54:30.092 day16_03[706:27193] 窗口被销毁了
2016-08-25 08:54:30.092 day16_03[706:27193] 编辑框被销毁了
2016-08-25 08:54:30.092 day16_03[706:27193] 按钮被销毁了
2016-08-25 08:54:30.092 day16_03[706:27193] ++++++
Program ended with exit code: 0
2.4  聚合是两个(以上)类的弱关系,不要求同生共死。(但也可以实现)。下面是聚合的例子

建一个Mouse类
.h文件

#import <Foundation/Foundation.h>

@interface CZMouse : NSObject

@end

.m文件

#import "CZMouse.h"

@implementation CZMouse
-(instancetype)init
{
    if (self = [super init])
    {
        NSLog(@"鼠标被创建了");
    }
    return self;
}
-(void)dealloc
{
    NSLog(@"鼠标被销毁了");
}
@end

建一个Computer类
.h文件

#import <Foundation/Foundation.h>
#import "CZMouse.h"

@interface CZComputer : NSObject
@property CZMouse *mouse; //要将部分类定义成属性
@end

.m文件

#import "CZComputer.h"

@implementation CZComputer
-(instancetype)init
{
    if (self = [super init])
    {
        NSLog(@"计算机被创建了");
    }
    return self;
}
-(void)dealloc
{
    NSLog(@"计算机被销毁了");
}
@end

main函数

#import <Foundation/Foundation.h>
#import "CZComputer.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        CZMouse *m = [[CZMouse alloc] init];
        NSLog(@"------");
        {
            CZComputer *com = [[CZComputer alloc] init];
            com.mouse = m;
           // CZMouse *m1 = [[CZMouse alloc] init];
           // com.mouse = m1;  
           //加上上边两行代码可以实现同生共死
        }  
        NSLog(@"======");
    }
    NSLog(@"++++++");
    return 0;
}

输出结果

2016-08-25 09:03:25.939 day16_05[747:30875] 鼠标被创建了
2016-08-25 09:03:25.940 day16_05[747:30875] ------
2016-08-25 09:03:25.940 day16_05[747:30875] 计算机被创建了
2016-08-25 09:03:25.940 day16_05[747:30875] 计算机被销毁了
2016-08-25 09:03:25.940 day16_05[747:30875] ======
2016-08-25 09:03:25.940 day16_05[747:30875] 鼠标被销毁了
2016-08-25 09:03:25.940 day16_05[747:30875] ++++++
Program ended with exit code: 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值