4.4OC10-内存管理2-set方法的内存管理
4.4OC10-内存管理2-set方法的内存管理
例一:
main.m
//
// main.m // OC10-内存管理2-set方法的内存管理 // // Created by qwz on 13-12-9. // Copyright (c) 2013年 renhe. All rights reserved. // #import <Foundation/Foundation.h> #import "Student.h" #import "Book.h" int main(int argc, const char * argv[]) { @autoreleasepool { Student *stu = [[Student alloc] initWithAge:29]; Book *book = [[Book alloc] initWithPrice:3.5]; [book release]; [stu release]; } return 0; } |
Student.h
//
// Student.h // OC10-内存管理2-set方法的内存管理 // // Created by liuyes on 13-12-9. // Copyright (c) 2013年 renhe. All rights reserved. // #import <Foundation/Foundation.h> #import "Book.h" @interface Student : NSObject @propertyint age; - (id)initWithAge:(int)age; @propertyBook *book; @end |
//
// Student.m // OC10-内存管理2-set方法的内存管理 // // Created by liuyes on 13-12-9. // Copyright (c) 2013年 renhe. All rights reserved. // #import "Student.h" @implementation Student #pragma mark 构造方法 - (id)initWithAge:(int)age{ if( self = [super init]){ _age = age; } returnself; } #pragma mark 回收对象 - (void)dealloc{ NSLog(@"student:%i 被销毁了", _age); [super release]; } @end |
//
// Book.h // OC10-内存管理2-set方法的内存管理 // // Created by liuyes on 13-12-9. // Copyright (c) 2013年 renhe. All rights reserved. // #import <Foundation/Foundation.h> @interface Book : NSObject @propertyfloat price; //价格 - (id)initWithPrice:(float)price; @end |
//
// Book.m // OC10-内存管理2-set方法的内存管理 // // Created by liuyes on 13-12-9. // Copyright (c) 2013年 renhe. All rights reserved. // #import "Book.h" @implementation Book - (id)initWithPrice:(float)price{ if (self = [super init]){ _price = price; } returnself; } - (void)dealloc{ NSLog(@"book:%f 被销毁了", _price); [super dealloc]; } @end |
运行结果
2013-12-09 17:04:29.679 OC10-内存管理2-set方法的内存管理[634:403] book:3.500000 被销毁了 2013-12-09 17:04:29.703 OC10-内存管理2-set方法的内存管理[634:403] student:29 被销毁了 |
//
// main.m
// OC10-内存管理2-set方法的内存管理
//
// Created by qwz on 13-12-9.
// Copyright (c) 2013年 renhe. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
#import "Book.h"
void test(Student *stu){
//book:1
Book *book = [[Book alloc] initWithPrice:3.5];
//book:2
stu.book = book;
//book:1
[book release];
Book *book2 = [[Book alloc] initWithPrice:4.5];
//book2:2
stu.book = book2;
//book2:1
[book2 release];
}
void test1(Student *stu){
[stu readBook];
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
//stu:1
Student *stu = [[Student alloc] initWithAge:29];
//stu:1
//book:1
//book2:1
test(stu);
//stu:1
//book:1
//book2:1
test1(stu);
//stu:0
//book:0
[stu release];
}
return 0;
}
Student.h
//
// Student.h
// OC10-内存管理2-set方法的内存管理
//
// Created by liuyes on 13-12-9.
// Copyright (c) 2013年 renhe. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Book.h"
@interface Student : NSObject{
Book *_book;
}
@propertyint age;
- (id)initWithAge:(int)age;
@propertyBook *book;
- (void)readBook;
@end
Student.m
//
// Student.m
// OC10-内存管理2-set方法的内存管理
//
// Created by liuyes on 13-12-9.
// Copyright (c) 2013年 renhe. All rights reserved.
//
#import "Student.h"
@implementation Student
#pragma mark 构造方法
- (id)initWithAge:(int)age{
if( self = [super init]){
_age = age;
}
returnself;
}
#pragma mark 回收对象
- (void)dealloc{
//释放对象
[_book release];
//[self.book release];
NSLog(@"student:%i 被销毁了", _age);
[super dealloc];
}
#pragma mark - getter和setter方法
//@synthesize book = _book;
//如果自己手动实现了getter和setter,xcode就不会自动生成@synthesize
//也就不会自动生成_book
//getter和setter的默认实现
- (void)setBook:(Book *)book{
if(_book != book){
//先释放旧的成员变量
[_book release];
//在retain新传进来的对象
_book = [book retain];
}
}
- (Book *)book{
return _book;
}
#pragma mark - 公共方法
#pragma mark 读书
- (void)readBook{
NSLog(@"当前读的书是:%f", _book.price);
}
@end
Book.h
//
// Book.h
// OC10-内存管理2-set方法的内存管理
//
// Created by liuyes on 13-12-9.
// Copyright (c) 2013年 renhe. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Book : NSObject
@propertyfloat price; //价格
- (id)initWithPrice:(float)price;
@end
Book.m
//
// Book.m
// OC10-内存管理2-set方法的内存管理
//
// Created by liuyes on 13-12-9.
// Copyright (c) 2013年 renhe. All rights reserved.
//
#import "Book.h"
@implementation Book
- (id)initWithPrice:(float)price{
if (self = [super init]){
_price = price;
}
returnself;
}
- (void)dealloc{
NSLog(@"book:%f 被销毁了", _price);
[super dealloc];
}
@end
4.4OC10-内存管理2-set方法的内存管理相关推荐
- 4用计算机显示内存不足,必成电脑:解决电脑提示“内存不足”四种方法-电脑内存不足怎么办...
1.打开的程序太多 如果同时打开的应用软件过多或者运行的文档过多,就没有足够的内存运行其他程序.这时,对于多文档界面(MDl)程序,如Wd.Excel等,请关闭当前文档外的所有文档,并退出当前未使用的 ...
- 软件需求管理用例方法 pdf_一卡通管理软件功能需求
一卡通管理软件功能需求 1)一卡通管理软件主要业务功能 根据分析,学院一卡通管理软件目前共考虑了如下功能: 消费类功能:餐厅消费.文体活动中心消费. 餐厅消费.文体活动中心消费的规则完成一致.即采用& ...
- 内存泄露、内存溢出以及解决方法
目录(?)[+] 内存泄露是指程序在运行过程中动态申请的内存空间不再使用后没有及时释放,从而很可能导致应用程序内存无线增长.更广义的内存泄露包括未对系统的资源的及时释放,比如句柄等. 内存溢出即用户在 ...
- 浅谈手机内存不够的解决方法--我的手机是红米note5
手机内存不够的解决方法 手机内存不够的解决方法 我为什么要关系内存问题? 应用搬到sd卡的方法 link2sd 刷机 注意 手机内存不够的解决方法 清理内存 大概卸载一些暂时不用的软件 我为什么要关系 ...
- android内存池,两种常见的内存管理方法:堆和内存池
描述 本文导读 在程序运行过程中,可能产生一些数据,例如,串口接收的数据,ADC采集的数据.若需将数据存储在内存中,以便进一步运算.处理,则应为其分配合适的内存空间,数据处理完毕后,再释放相应的内存空 ...
- 关于内存管理/set/get方法
MRC状态下 1 任何继承NSObject的对象,存放于堆控件中,都需要手动管理内存 . 2 基本数据类型放到栈中,对象放到堆空间中,内存是有系统管理的.(int\float\enum\struct) ...
- python内存管理方法_Python 内存管理大揭秘
前言 语言的内存管理是语言设计的一个重要方面.它是决定语言性能的重要因素.无论是C语言的手工管理,还是Java的垃圾回收,都成为语言最重要的特征.这里以Python语言为例子,说明一门动态类型的.面向 ...
- linux内存管理的主要概念是虚拟内存,你知道linux内存管理基础及方法?
描述 一.基本概念 (1)物理内存和虚拟内存 物理内存:系统硬件提供的真实物理内存 虚拟内存:利用磁盘空间虚拟出的一块逻辑内存,用作虚拟内存的磁盘空间被称为swap,swap类似于windows的虚拟 ...
- java方法区内存泄露_深入理解java虚拟机-第二章:java内存区域与内存泄露异常...
2.1概述: java将内存的管理(主要是回收工作),交由jvm管理,确实很省事,但是一点jvm因内存出现问题,排查起来将会很困难,为了能够成为独当一面的大牛呢,自然要了解vm是怎么去使用内存的. 2 ...
最新文章
- mysql insert concat_MySQL常用内置函数说明+concat+insert(str,m,n,inser_str)+week(now())+ | 学步园...
- python画折线图代码-python画折线示意图实例代码
- 【NLP】NLP任务增强:通过引入外部知识来提供额外信息
- Threading in C#
- 美国国土安全部发布物联网安全最佳实践
- day32 java的多线程(2)
- 个人管理 - Learn More,Study Less!
- 游戏 TRAP(SNRS)AlphaBeta版本
- 面试—每日一题(7)
- hdu 1059 (多重背包) Dividing
- 前端面试宝典(4)——必掌握
- Atitis mybatis的功能api扩展总结 目录 1. MybatisAdvUtil	1 1.1. 根据session得到所有配置	1 1.2. Configuration1.getMappe
- 【图像几何】基于matlab GUI图像几何运算系统【含Matlab源码 206期】
- 浏览器兼容video视频播放的多种方法
- #9733;用辩证数学解答“缸中之脑”
- 如何在word中的方框里打钩
- 超标量处理器设计 姚永斌 第7章 寄存器重命名 摘录
- 等高线节点过密如何处理?CASS10.1复合线滤波功能详解
- QT之 QSQLite
- 光线微弯传感器matlab仿真,基于输出光斑旋转的光纤微弯位移传感器
热门文章
- 苹果更新一半能取消吗_苹果股价最新行情-iPhone12一半用户选蓝色 苹果股价还会继续涨吗?...
- 广州塔C语言程序,小蛮腰 led广州塔diy程序
- 切断计算机主机电源,不用电脑时,请彻底切断电源,否则……
- 中兴c300业务板_中兴OLT C300板卡添加
- ibm服务器修改uefi和legacy,uefi和legacy的区别详细分析(附带uefi改legacy教程)
- 2.12-3.20上周的习惯坚持下来了✌️精诚所至金石为开,加油兄弟
- Webdings字体和Wingdings字体对照表
- 电脑如何关闭445端口
- 关于城市旅游的HTML网页设计——(旅游风景云南 5页)HTML+CSS+JavaScript
- 关于rpm安装安装包是出现的error: Failed dependencies:解决办法