Create a class book
. h has @ property, float price; @synthesize Automatic
------------
Create a class Student
#import "Book.h"
. h has @property int age;
@property book *book; @synthesize Automatic
--------------
Verifying that the Student object is recycled
Student.m
-(void) dealloc{
NSLog (@ "Student:%i was destroyed", _age);
[Super Dealloc];
}
--------------
To facilitate access to _age create a construction method that can pass in _age
Student.h
-(ID) initwithage: (int) age;
Student.m
-(ID) initwithage: (int) age{
if (Self=[super init]) {//If Super returns an object that is not empty
_age=age;
}
return self;
}
----------------------
Verifying that the book object is recycled
Book.m
-(void) dealloc{
NSLog (@ "Book:%f was destroyed", _price);
[Super Dealloc];
}
----------------------------
With _price book constructor
Book.h
-(void) Initwithprice: (float) Price;
Book.m
-(void) Initwithprice: (float) price{
if (Self=[super init]) {//If Super returns an object that is not empty
_price=price;
}
}
----------------------------
Annotation method
1 #pragma Mark Constructor
(advantage:easy:locate)
2 #pragma mark -groupname
(Advantage:group)
3 of course//can work
----------------------------
Main.m
#import "Book.h"
#import "Student.h"
Main () {
@autoreleasepool {
Student *stu=[[student Alloc]initwithage:10]; Stu 1
Book *book =[[book alloc]initwithprice:3.5];//Book 1
stu.book=book;//in reality never with this Way//book 1
[Book Release];//book 0
[Stu Release];//stu 0
}
return 0;
}
---------------------------------
Student.m
Manually realize Getter & Setter
Standard realization of Getter and setter
Student.h//Because u do manually so XCode won't call @synthesize, so there are no//_book,so we need to state _book Ourself.
@interface student:nsobject{
Book *_book;
}
Student.m
-(void) Setbook: (book *) book{
_book=book;
}
-(book *) book{
return _book;
}
Abopve Six line can is short for
@synthesize Book=_book;
---------------------------------------
Then counter
-----------------------------------------
When we developing in reality, we could use object
So change code
Main.m
void Test (Student *stu) {
Book *book=[[book alloc]initwithprice:3.5];
Stu.book=book;
[Book release];
}
void Test1 (Student *stu) {
Add Reading method
Student.h-(void) Readbook;
Student.m
-(void) readbook{
NSLog (@ "Reading book is:%f", _book.price);
//}
[Stu readbook];//not safe, visiting _book,because in test book was released so (wild pointer)
}
-------------------------
Change code
Main () {
Test (STU);
Test1 (Stu);
}
-----------------------------
Student want to make use of the book object So, retain book in student
Setbook Retain;stu alloc Release
---------------------------------
Improve Code
So compare the passed book object with a current book object if not the same one,
Release the previous one.
-(void) Setbook: (book *) book{
if (Self.book!=book) {
[_book release]; Empty pointer is safe.
_book=[book retain];
}
}
Be released (-1) and destroyed (0) different
Memory management of memory management 2-set Method-Program parsing