Object-c学习之路四(oc内存管理autorelease)


再以Student和Book为例作为展示:

1.主函数:

//  main.m
//  MemoryManagement2
//
//  Created by WildCat on 13-7-24.
//  Copyright (c) 2013年 wildcat. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Book.h"
#import "Student.h"
int main(int argc, const char * argv[])
{

    @autoreleasepool {
        Student *stu=[[[Student alloc] init] autorelease];
        Book *book=[[[Book alloc] init] autorelease];
        stu.book=book;
        
        
        Student *stu2=[Student student];
        Student *stu3=[Student studentWithAge:20];    
    }
    return 0;
}


2.Student函数:

//  Student.h
//  MemoryManagement2
//
//  Created by WildCat on 13-7-24.
//  Copyright (c) 2013年 wildcat. All rights reserved.
//

#import <Foundation/Foundation.h>
@class Book;
@interface Student : NSObject
#pragma mark - 属性定义
#pragma mark @property关键字的 用法
//nonatomic表示不支持多线程 atomic表示支持多线程
@property (nonatomic,retain) Book *book;
@property (nonatomic,setter = isReach:) BOOL rich;
@property (nonatomic,assign) int age;
#pragma mark - 静态方法快速创建
+(id)student;
+(id)studentWithAge:(int)age;

@end


#import "Student.h"
#import "Book.h"
@implementation Student
#pragma mark - 销毁对象
-(void)dealloc{

    //销毁Book
    NSLog(@"Student %@ ,被销毁。",self);
    self.book=nil;
    [super dealloc];
}
#pragma mark - 静态方法快速创建对象
+(id)student{
    return [[[Student alloc] init] autorelease];
}
+(id)studentWithAge:(int)age{
    Student *stu=[self student];
    stu.age=age;
    return stu;
}

@end



3.Book函数略







转载于:https://www.cnblogs.com/lixingle/p/3312981.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值