首页 技术 正文
技术 2022年11月12日
0 收藏 499 点赞 3,061 浏览 3294 个字

手机内存下的类的设计练习:

设计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的时候,真正去销毁对象。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,738
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,129
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,292