#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[])
{
Watermelon *wat = [[Watermelon alloc]init];
[wat setVariety:@"?"];
Person *p1 = [[Person alloc]init];
[p1 setName:@"tom"];
[p1 setWatermelon:wat];
Person *p2 = [[Person alloc]init];
[p2 setName:@"james"];
[p2 setWatermelon:wat];
Person *p3 = [[Person alloc]init];
[p3 setName:@"jack"];
[p3 setWatermelon:wat];
Person *p4 = [[Person alloc]init];
[p4 setName:@"wade"];
[p4 setWatermelon:wat];
Person *p5 = [[Person alloc]init];
[p5 setName:@"jj"];
[p5 setWatermelon:wat];
NSLog(@"--%lu--",[wat retainCount]);//计数器=6
[wat release];//-1
wat = nil;
[p1 release];//-1
p1 = nil;
[p2 release];//-1
p2 = nil;
[p3 release];//-1
p3 = nil;
[p4 release];//-1
p4 = nil;
[p5 release];//-1
p5 = nil;
return 0;
}
#import <Foundation/Foundation.h>
#import "Watermelon.h"
@interface Person : NSObject//创造了人的类
@property(assign,nonatomic)NSString *name;
@property(retain,nonatomic)Watermelon *watermelon;
@end
#import "Person.h"
@implementation Person
-(void)dealloc
{
NSLog(@"%@吃%@",_name,[_watermelon variety]);
[_watermelon release];
[super dealloc];
}
@end
#import <Foundation/Foundation.h>
@interface Watermelon : NSObject//创造了西瓜的类
@property(assign,atomic)NSString *variety;
@end
#import "Watermelon.h"
@implementation Watermelon
-(void)dealloc
{
NSLog(@"%@吃完了",_variety);
[super dealloc];
}
@end
转载于:https://my.oschina.net/u/2426724/blog/485773