#import <Founation\Founation.h> //定义书类
@interface Book: NSObject
@property int price;
-(void)read;
@end
@import "Book.h"
@implementation
-(void)read{
NSLog("reading book");
}
@end
#import <Founation\Founation.h>
@class Book;
@interface Student:NSObject{
Book* _book;
}
@property int age;
@property Book* book
@end
#import "Student.h"
#import"Book.h"
@implementation Student
@synthesize age=_age;
-(id)setBook:(Book *)book{
if(_book!=book){
[_book release];
_book=[book retain];
}
-(Book *)book{
return _book;
}
}
-(void)dealloc{
[_book release];
[super dealloc];
}
@end
在以下代码中:
-(id)setBook:(Book *)book{
if(_book!=book){
[_book release];
_book=[book retain];
}
使用 set管理内存 时 是先release 旧对象 retain 新对象 ,问题是 刚进程序初始化的时候 _book没有值啊
还要去release 怎么不会报野指针错误 不解 求大神帮忙? |