您的位置:首页 > 移动开发 > Objective-C

OC学习笔记(8)Object-C组合使用

2014-12-23 12:38 197 查看
1、ASPoint类

//.h
#import <Foundation/Foundation.h>

@interface ASPoint : NSObject
@property(nonatomic,assign) double x;
@property(nonatomic) double y;
-(id) initWithX:(double)aX y:(double)aY;
-(NSString*) description;
@end
//.m
#import "ASPoint.h"

@implementation ASPoint
@synthesize x,y;
-(id) initWithX:(double)aX y:(double)aY
{
if(self = [super init])
{
x = aX;
y = aY;
}
return self;
}

-(NSString*) description
{
return [NSString stringWithFormat:@"(%g,%g)",x,y];
}
@end


2、ASRectangle类
//.h
#import <Foundation/Foundation.h>
@class ASPoint;
@interface ASRectangle : NSObject
{
ASPoint* origion;
double width;
double height;
}
//copy产生传递过来的对象的副本
//@property(nonatomic,retain) ASPoint* origin;
@property(nonatomic,copy) ASPoint* origin;
@property(nonatomic) double width;
@property(nonatomic) double height;

-(id) initWithOrigin:(ASPoint*)aPoint width:(double)aWidth height:(double)aHeight;
-(double) area;

@end
//.m
#import "ASRectangle.h"
#import "ASPoint.h"
@implementation ASRectangle
@synthesize origin,width,height;

-(id) initWithOrigin:(ASPoint *)aPoint width:(double)aWidth height:(double)aHeight
{
if(self = [super init])
{
ASPoint* tmp = [[ASPoint alloc] initWithX:aPoint.x y:aPoint.y];
origin = tmp;//copy

//        origin = [aPoint retain];//retain
width = aWidth;
height = aHeight;
}
return self;
}

-(id) init
{
return [self initWithOrigin:nil width:0 height:0];
}

-(double) area
{
return width * height;
}
-(NSString*) description
{
return [NSString stringWithFormat:@"%@,%g %g",origin,width,height];
}
-(void) setOrigin:(ASPoint *)aOrigin
{
if(origin != aOrigin)
{
[origin release];
origin = [aOrigin retain];
}
}

-(void) dealloc
{
self.origin = nil;
[super dealloc];
}
@end

//main.m
#import <Foundation/Foundation.h>
#import "ASPoint.h"
#import "ASRectangle.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {

ASPoint* p1 = [[ASPoint alloc] initWithX:2 y:3];
ASRectangle* rec = [[ASRectangle alloc] initWithOrigin:p1 width:10 height:15];
[rec area];
NSLog(@"%g",[rec area]);
NSLog(@"%lu",(unsigned long)p1.retainCount);
NSLog(@"%@",rec.origin);

p1.x = 4;
NSLog(@"%@",rec.origin);
NSLog(@"%@",p1.description);
[rec release];//必须重载dealloc,两个类之间有hasa关系,声明属性使用retain,并重载dealloc
NSLog(@"%lu",(unsigned long)p1.retainCount);
[p1 release];

//
}
return 0;
}


打印结果
1、retain

2014-12-23 12:37:22.439 test2[1789:64000] 150
2014-12-23 12:37:22.441 test2[1789:64000] 2
2014-12-23 12:37:22.441 test2[1789:64000] (2,3)
2014-12-23 12:37:22.441 test2[1789:64000] (4,3)
2014-12-23 12:37:22.441 test2[1789:64000] (4,3)
2014-12-23 12:37:22.441 test2[1789:64000] 12、copy
2014-12-23 12:38:05.795 test2[1804:64412] 150
2014-12-23 12:38:05.796 test2[1804:64412] 1
2014-12-23 12:38:05.796 test2[1804:64412] (2,3)
2014-12-23 12:38:05.796 test2[1804:64412] (2,3)
2014-12-23 12:38:05.796 test2[1804:64412] (4,3)
2014-12-23 12:38:05.797 test2[1804:64412] 1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  OC 组合
相关文章推荐