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;
}
|
|