A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

import<Foundation/Foundation.h>

@interface Student :NSObject
{
int _no;
NSString *_name;
Car *_car;
Dog *_dog;
}

-(void)setNo:(int)no;
-(int)no;
-(void)setName:(NSString *)name;
-( NSSTring *)name;
-(void)setCar:(Car *)car;
-(Car *)car;
-(void)setDog:(Dog *)dog;
-(Dog *)dog;





@end

@implementation Student
-(void)setNo:(int)no
{
_no=no;
}
-(int)no
{

return _no;
}
-(void)setName:(NSString *)name
{
if(_name!=name)
{
[_name release];
_name= [name retain];


}

}

-( NSSTring *)name
{
return _name;
}
-(void)setCar:(Car *)car
{
if(_car!=car)
{
[_car release];
_car= [car retain];


}

}
-(Car *)car
{


return _car;
}
-(void)setDog:(Dog *)dog
{
if(_dog!=dog)
{
[_dog release];
_dog= [dog retain];


}

}
-(Dog *)dog
{

return dog;
}

-(void)delloc
{
//拥有几个对象成员变量,就做几个成员变量的release
[_name release];
[_car release];
[_dog release];

  

[super delloc];
}


@end
//Car对象
@interface Car :NSObject


@end

@implementation Car


@end
//Dog对象

@interface Dog :NSObject


@end

@implementation Dog


@end




int main()
{
Student *stu =[Student alloc]  init];
  
  Car * c = [[Car alloc] init];
     stu.car=c;
  Dog *d = [[Dog alloc] init]
      stu.dog=d;
     
   NSString *s =@"Jack";//凡是对象不是通过alloc产生的,就不需要release
   stu.name = s;

  
  [d release];

   [c release];
   [stu  release];







return 0;
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马