New->New Project->OS X(Application)->Command Line Tool->(程序名:PersonAlloc )->右键main.m->new File->iOS(Cocoa Touch)->Objective-C class->(类名:Person:NSObject)
Person.h
@interface Person
{
int age;
int identify;
}
-(void)initWithAge:(int)_age:(int)_identify;//初始化年龄、证号
-(int)getAge;
-(void)setAge:(int)_age;
-(int)getIdentify;
-(void)setIdentify:(int)_identify;
//新建函数
-(void)changeArgPointNew:(Person*)_person;//改变指针参数(把新参数分配alloc)
-(void)changeArgPoint:(Person*)_person;//改变指针参数,参数就是main里传递过来的对象指针
@end
Person.m
@implementation Person
-(void)initWithAge:(int)_age:(int)_identify//初始化年龄、证号
{
if(self=[super init])//父类初始化,返回子类的指针
{
age=_age;
identify=_identify;
}
return self;
}
-(int)getAge
{
return age;
}
-(void)setAge:(int)_age
{
age=_age;
}
-(int)getIdentify
{
return identify;
}
-(void)setIdentify:(int)_identify
{
identify=_identify;
}
//新建函数
-(void)changeArgPointNew:(Person*)_person//改变指针参数(把新参数分配alloc)
{
_person=[ [Person alloc]initWithAge : 22 : 5343433344 ];//只是初始化形参
NSLog("%_person临时参数的地址:%p" , _person );//打印形参的地址 函数执行完毕后,_person自动消失,指向的内存空间(堆里)手动释放。
}
-(void)changeArgPoint:(Person*)_person//改变指针参数,参数就是main里传递过来的对象指针
{
[ _person setAge : 22 ];//修改的是形参(其实也是类的对象)的指针所指向的内存数据
}
@end
main.m
int main(int argc,chr *argv[])
{
Person *p1=[[Person alloc]initWithAge:35:892355];
Person *p2=[[Person alloc]initWithAge:25:545443];
Person *p=[[Person alloc]initWithAge:0:000000];
[p changeArgPointNew:p1];
NSLog("p1的地址:%p,p1的年龄:%d",p1,[p1 getAge]);
[p changeArgPoint:p2];
NSLog("p2的地址:%p,p2的年龄:%d",p2,[p2 getAge]);
[p1 release];
[p2 release];
[p release];
}
[p changeArgPointNew:p1]函数执行内存分析:
第二个函数执行内存分析: