TA的每日心情 | 汗 2024-10-15 10:05 |
---|
签到天数: 372 天 [LV.9]以坛为家II
|
本帖最后由 Sian 于 2014-1-18 18:31 编辑
需求分析:
写一个Rectangle类,具备以下属性:位置(origin)、长(width)、宽(height)
要求提供:初始化方法、工厂方法、业务方法(移动位置、求周长、求面积)
程序实现:说明:我们程序实现中写两个类SAPoint与SARectangle,SAPoint专为矩形提供位置服务(矩形的一个属性)
SAPoint.h- //
- // SAPoint.h
- // Rectangle
- //
- // Created by yusian on 14-1-18.
- // Copyright (c) 2014年 yusian. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @interface SAPoint : NSObject
- @property int x;
- @property int y;
- - (id) init;
- - (id) initWithX:(int)x andY:(int)y;
- + (id) pointWithX:(int)x andY:(int)y;
- - (void) show;
- @end
复制代码 SAPoint.m- //
- // SAPoint.m
- // Rectangle
- //
- // Created by yusian on 14-1-18.
- // Copyright (c) 2014年 yusian. All rights reserved.
- //
- #import "SAPoint.h"
- @implementation SAPoint
- - (id) init
- {
- if (self = [super init]) {
- _x = 1;
- _y = 1;
- }
- return self;
- }
- - (id) initWithX:(int)x andY:(int)y
- {
- if (self = [super init]) {
- self.x = x;
- self.y = y;
- }
- return self;
- }
- + (id) pointWithX:(int)x andY:(int)y
- {
- return [[SAPoint alloc] initWithX:(int)x andY:(int)y];
- }
- - (void) show{
- NSLog(@"The position is (%d,%d)",_x,_y);
- }
- - (void) dealloc
- {
- NSLog(@"SAPoinr is dealloc");
- [super dealloc];
- }
- @end
复制代码 SARectangle.h- //
- // SARectangle.h
- // Rectangle
- //
- // Created by yusian on 14-1-18.
- // Copyright (c) 2014年 yusian. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "SAPoint.h"
- @interface SARectangle : NSObject
- @property int width;
- @property int height;
- @property (assign) SAPoint *origin;
- - (id) initWithPoint:(SAPoint *)origin andWidth:(int)width andHeight:(int)height;
- + (id) rectangleWithPoint:(SAPoint *)origin andWith:(int)width andHeight:(int)height;
- - (void) show;
- - (void) moveToX:(int)x andY:(int)y;
- - (int) perimeter;
- - (void) perimeterPrint;
- - (int) area;
- - (void) areaPrint;
- @end
复制代码 SARectangle.m- //
- // SARectangle.m
- // Rectangle
- //
- // Created by yusian on 14-1-18.
- // Copyright (c) 2014年 yusian. All rights reserved.
- //
- #import "SARectangle.h"
- @implementation SARectangle
- - (id) initWithPoint:(SAPoint *)point andWidth:(int)width andHeight:(int)height
- {
- self = [super init];
- if (self) {
- self.origin = point;
- self.width = width;
- self.height = height;
- }
- return self;
- }
- + (id) rectangleWithPoint:(SAPoint *)point andWith:(int)width andHeight:(int)height
- {
- return [[SARectangle alloc] initWithPoint:(SAPoint *)point andWidth:(int)width andHeight:(int)height];
- }
- - (void) moveToX:(int)x andY:(int)y
- {
- _origin.x = x;
- _origin.y = y;
- NSLog(@"The new position is (%d,%d)",_origin.x,_origin.y);
- }
- - (int) perimeter
- {
- return (self.width+self.height)*2;
- }
- - (void) perimeterPrint
- {
- NSLog(@"The Perimeter is %d",self.perimeter);
- }
- - (int) area
- {
- return self.width*self.height;
- }
- - (void) areaPrint
- {
- NSLog(@"The area is %d",self.area);
- }
- - (void) show
- {
- NSLog(@"The rectangle position is (%d,%d) , Width is %d , Height is %d",_origin.x,_origin.y,self.width,self.height);
- }
- - (void) dealloc
- {
- NSLog(@"SARectangle is dealloc");
- [_origin release];
- [super dealloc];
- }
- @end
复制代码 main.m- //
- // main.m
- // Rectangle
- //
- // Created by yusian on 14-1-18.
- // Copyright (c) 2014年 yusian. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "SAPoint.h"
- #import "SARectangle.h"
- int main(int argc, const char * argv[])
- {
-
- @autoreleasepool {
-
- SAPoint * point = [SAPoint pointWithX:2 andY:4];//工厂方法
- [point show];
-
- SARectangle * rectangle = [SARectangle rectangleWithPoint:point andWith:10 andHeight:20];
-
- [rectangle show];
- [rectangle perimeterPrint];
- [rectangle areaPrint];
- [rectangle moveToX:3 andY:5];
- [rectangle release];
-
- }
- return 0;
- }
复制代码 输出结果:- 2014-01-18 18:25:40.178 Rectangle[6671:303] The position is (2,4)
- 2014-01-18 18:25:40.179 Rectangle[6671:303] The rectangle position is (2,4) , Width is 10 , Height is 20
- 2014-01-18 18:25:40.180 Rectangle[6671:303] The Perimeter is 60
- 2014-01-18 18:25:40.180 Rectangle[6671:303] The area is 200
- 2014-01-18 18:25:40.180 Rectangle[6671:303] The new position is (3,5)
- 2014-01-18 18:25:40.181 Rectangle[6671:303] SARectangle is dealloc
- 2014-01-18 18:25:40.181 Rectangle[6671:303] SAPoinr is dealloc
- Program ended with exit code: 0
复制代码
|
|