ios视频学习3.3 类的声明和创建内存分析2


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]函数执行内存分析:


第二个函数执行内存分析:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值