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
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
打印结果
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
//.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
相关文章推荐
- es6的Object.keys()和map()组合使用的案例
- Object 监视器方法分解成截然不同的对象, 以便通过将这些对象与任意Lock 实现组合使用,为每个对象提供多个等待 set
- OC学习笔记(9)Object-C多态的使用
- [Object]面向对象编程(高程版)(三)组合使用构造函数和原型模式
- 在VC中使用FileSystemObject
- XML HTTP Request object简单使用
- 如何禁止恶意用户使用FileSystemObject
- [ZT] 使用UML时,必须从functional 思维转移到object思维,应如何练习呢?
- XML HTTP Request object简单使用
- MapObject控件的使用之加入图层
- asp中使用FileSystemObject
- Flex Remote Object中直接使用HttpSession的方法
- 在ASP.NET(C#)中使用Oralce Object For OLE 2.3连接Oracle 8.0.5数据库!
- 关于Data Access Blok (SqlHelper.cs)使用时出现"对象必须实现 IConvertible/Object must implement IConvertible."异常
- AggregateCacheDependency、CacheDependency、SqlCacheDependency Asp.net 2.0和Sql Server的缓存管理和使用ObjectBuilder改造PetShop4 的缓存示例
- Flex Remote Object中直接使用HttpSession的方法
- 对不变的data和object references 使用final(注意这里指的是references而不是对象)
- ASP中使用Oracle Object For Ole连接Oracle数据库
- 使用XMLHTTP Request Object获取服务器数据
- AggregateCacheDependency、CacheDependency、SqlCacheDependency Asp.net 2.0和Sql Server的缓存管理和使用ObjectBuilder改造PetShop4 的缓存示例