手机内存下的类的设计练习:
设计Book类,1.三个成员变量: title(书名)author(作者)、price(价格)2.不使用@property,自己完成存取方法(set方法,get方法)3、加入必要其他的方法4、并对Book类进行测试 .h声明文件
// Book.h
// 引用计数器
//
// Created by ma c on 15/8/13.
// Copyright (c) 2015年. All rights reserved.
// #import <Foundation/Foundation.h> @interface Book : NSObject
{
NSString *_title;
NSString *_author;
CGFloat _price;
}
-(id)initWithTitle:(NSString*)title andAuthor:(NSString*)author
AndPrice:(CGFloat)price;
-(void)setTitle:(NSString*) title;
-(void)setAuthor:(NSString*) author;
-(void)setPrice:(CGFloat) price;
-(NSString*) title;
-(NSString*) author;
-(CGFloat) price;
-(void) show;
@end
.m声明文件
// Book.m
// 引用计数器
//
// Created by ma c on 15/8/13.
// Copyright (c) 2015年. All rights reserved.
// #import "Book.h" @implementation Book
-(id)initWithTitle:(NSString*)title andAuthor:(NSString*)author
AndPrice:(CGFloat)price
{
self = [super init];
if(self)
{
_title = [title retain];
_author = [author retain];
_price = price;
}
return self;
}
-(void)setTitle:(NSString*) title
{
if(_title != title)
{
[_title release];//释放上一次拥有的对象所有权
_title = [title retain];//获取这一次的对象所有权
}
}
-(void)setAuthor:(NSString*) author
{
if(_author != author)
{
[_author release];//释放上一次拥有的对象所有权
_author = [author retain];//获取这一次的对象所有权
}
}
-(void)setPrice:(CGFloat) price
{
_price = price;
}
-(NSString*) title
{
return _title;
}
-(NSString*) author
{
return _author;
}
-(CGFloat) price
{
return _price;
}
-(void) show
{
NSLog(@"title:%@,author:%@,price:%.2f",_title,_author,_price);
}
-(void)dealloc
{
[_title release];
[_author release];
NSLog(@"title retainCount:0");
NSLog(@"author retainCount:0");
NSLog(@"book retainCount:0");
NSLog(@"book is dealloc!");
[super dealloc];
}
@end
测试Book类
// main.m
// 引用计数器
//
// Created by ma c on 15/8/13.
// Copyright (c) 2015年. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Book.h"
int main(int argc, const char * argv[])
{
//@autoreleasepool { //创建书对象book并初始化
Book *book = [[Book alloc]initWithTitle:@"OC" andAuthor:@"Jobs" AndPrice:35.6];//book count:1
NSLog(@"book retainCount:%lu",[book retainCount]); //创建书名对象title
NSMutableString *title = [NSMutableString stringWithString:@"IOS"];//title count:1
NSLog(@"title retainCount:%lu",[title retainCount]); //设置书名
[book setTitle: title];//title count:2
NSLog(@"title retainCount:%lu",[title retainCount]); //创建书的作者对象author
NSMutableString *author = [NSMutableString stringWithString:@"Bill"];//author count:1
NSLog(@"author retainCount:%lu",[author retainCount]); //设置书的作者名
[book setAuthor:author];//author count:2
NSLog(@"author retainCount:%lu",[author retainCount]); //设置书的价格
[book setPrice:58.9]; //释放title对象所有权----与上面的创建title对象相对应
[title release];//title count:1
NSLog(@"title retainCount:%lu",[title retainCount]); //释放author对象所有权----与上面的创建author对象相对应
[author release];//author count:1
NSLog(@"author retainCount:%lu",[author retainCount]); //释放在book类中的成员实例变量title和author对象的所有权,并销毁book对象
[book show];
[book release];//title count:0, author count:0 ,book count:0, dealloc book
//}
return ;
}
运行结果:
-- ::49.608 引用计数器[:] book retainCount:
-- ::49.609 引用计数器[:] title retainCount:
-- ::49.610 引用计数器[:] title retainCount:
-- ::49.610 引用计数器[:] author retainCount:
-- ::49.610 引用计数器[:] author retainCount:
-- ::49.610 引用计数器[:] title retainCount:
-- ::49.610 引用计数器[:] author retainCount:
-- ::49.610 引用计数器[:] title:IOS,author:Bill,price:58.90
-- ::49.611 引用计数器[:] title retainCount:
-- ::49.611 引用计数器[:] author retainCount:
-- ::49.611 引用计数器[:] book retainCount:
-- ::49.611 引用计数器[:] book is dealloc!
Program ended with exit code:
可以看出:
计数器:retainCount 对象中存储被引用的次数, 当被引用的时候,计数器加1; 不在引用的时候,计数器减1; 当计数器为0的时候,真正去销毁对象。