内存管理2对象之间的内存管理
来源:互联网 发布:淘宝宠物疫苗曝光 编辑:程序博客网 时间:2024/04/29 00:37
Book.h:
#import <Foundation/Foundation.h>@interface Book : NSObject@property float price;-(id)initeWithPrice:(float)price;@end
Book.m:
#import "Book.h"@implementation Book#pragma mark 构造方法-(id)initeWithPrice:(float)price{ if(self=[super init]) { _price=price; } return self;}#pragma mark 回收方法-(void)dealloc{ NSLog(@"Book:%f 被销毁了",_price); [super dealloc];}@end
Student.h:
#import <Cocoa/Cocoa.h>@class Book;@interface Student : NSObject{ Book * _book;}@property int age;-(id)initWithAge:(int)age;@property Book * book;-(void)readBook;@end
Student.m:
#import "Student.h"#import "Book.h"@implementation Student#pragma mark - 生命周期方法-(id)initWithAge:(int)age{ if(self=[super init]) { _age=age; } return self;}-(void)dealloc{ [_book release]; NSLog(@"Student: %i被销毁了",_age); [super dealloc];}//如果手动设置getter和setter方法 Xcode就不会帮我们自动生成synthesize,也就不会自动生成带下划线的属性#pragma mark - 设置getter和setter方法#pragma mark setter方法-(void)setBook:(Book *)book{ if(_book!=book) { //先释放旧对象,再retain新对象 [_book release];//OC里面没有空指针释放错误 [nil release]不报错 //book:1 _book=[book retain]; }}#pragma mark getter方法-(Book *)book{ return _book;}#pragma mark - 公共方法#pragma mark 读书-(void)readBook{ NSLog(@"当前读的书的价格是:%f",_book.price);}#pragma mark - 私有方法#pragma mark 私有方法1-(void)test1{ }#pragma mark 私有方法2-(void)test2{}#pragma mark 私有方法3-(void)test3{}@end
main:
#import <Foundation/Foundation.h>#import "Student.h"#import "Book.h"void test(Student * stu){ //book:1 Book * book=[[Book alloc] initeWithPrice:11.2]; //book:2 stu.book=book; //book:1 [book release]; Book * book1=[[Book alloc] initeWithPrice:22.2]; stu.book=book1; [book1 release];}void test1(Student *stu){ [stu readBook];}int main(int argc, const char * argv[]){ @autoreleasepool {// Student *stu=[[[Student alloc] initWithAge:10] autorelease];// NSLog(@"Student age is %i",stu.age);// Book *book=[[Book alloc] initeWithPrice:11.1f];// NSLog(@"Book price is %.1f",book.price);//// stu.book=book;// NSLog(@"%@",stu.book);// [book release]; //计数器 //stu 1 Student *stu=[[Student alloc] initWithAge:10]; //book:1 //stu:1 test(stu); //book:1 //stu:1 test1(stu); //stu:0 //book:0 [stu release]; //在释放stu时候也释放book,因为stu在setter的时候retain了一下,就由它释放 Student * stu1=[[[Student new]initWithAge:20] autorelease]; test(stu1); } return 0;}结果:
2013-08-02 15:01:11.571 内存管理2 a对象之间的内存管理[832:303] Book:11.200000被销毁了
2013-08-02 15:01:11.573 内存管理2 a对象之间的内存管理[832:303]当前读的书的价格是:22.200001
2013-08-02 15:01:11.573 内存管理2 a对象之间的内存管理[832:303] Book:22.200001被销毁了
2013-08-02 15:01:11.574 内存管理2 a对象之间的内存管理[832:303] Student: 10被销毁了
2013-08-02 15:01:11.574 内存管理2 a对象之间的内存管理[832:303] Book:11.200000被销毁了
2013-08-02 15:01:11.574 内存管理2 a对象之间的内存管理[832:303] Book:22.200001被销毁了
2013-08-02 15:01:11.575 内存管理2 a对象之间的内存管理[832:303] Student: 20被销毁了
- 内存管理2对象之间的内存管理
- ios内存管理2-对象之间的内存管理
- ios内存管理2-对象之间的内存管理
- 多个对象之间的内存管理
- OC学习笔记四-2 内存管理(对象之间的内存管理)
- 对象的内存管理
- IOS开发基础Object-C(08)—OC内存管理(2)-对象之间的内存管理
- 8.多个对象之间的内存管理
- OC 多个对象之间的内存管理
- Objective-C - 多个对象之间的内存管理
- Object-c------多个对象之间的内存管理
- Cocos2dx 对象的内存管理2
- Nib 对象的内存管理
- OC对象的内存管理
- 对象内存管理的学习
- 多对象的内存管理
- 单个对象的内存管理 内存泄露
- OC内存管理01--对象的内存管理
- UIAlertView备忘
- hdu 1019 Least Common Multiple
- 使用 Python 获取 Linux 系统信息
- 运行地址
- oracle的dbms_scheduler
- 内存管理2对象之间的内存管理
- (13)拨云见日,闲聊哈希表
- Linux新手入门之如何登入登出Linux操作系统
- 设计模式之适配器模式(Adapter)
- 完全删除SQL server 2008
- POJ 3009 Curling 2.0
- Linux关机命令详解
- Chrome不支持showModalDialog模态对话框和无法返回returnValue的问题
- 一个python远程机群计算的垃圾代码