Sian 发表于 2014-1-18 18:29:17

业务练习(矩形)

本帖最后由 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 = ) {
      _x = 1;
      _y = 1;
    }
    return self;
}

- (id) initWithX:(int)x andY:(int)y
{
    if (self = ) {
      self.x = x;
      self.y = y;
    }
    return self;
}

+ (id) pointWithX:(int)x andY:(int)y
{
    return [ initWithX:(int)x andY:(int)y];
}

- (void) show{
    NSLog(@"The position is (%d,%d)",_x,_y);
}

- (void) dealloc
{
    NSLog(@"SAPoinr is dealloc");
    ;
}

@endSARectangle.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;
@endSARectangle.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 = ;
    if (self) {
      self.origin = point;
      self.width = width;
      self.height = height;
    }
    return self;
}
+ (id) rectangleWithPoint:(SAPoint *)point andWith:(int)width andHeight:(int)height
{
    return [ 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");
    ;
    ;
}

@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 = ;//工厂方法
      ;
      
      SARectangle * rectangle = ;
      
      ;
      ;
      ;
      ;
      ;
      
    }
    return 0;
}
输出结果:
2014-01-18 18:25:40.178 Rectangle The position is (2,4)
2014-01-18 18:25:40.179 Rectangle The rectangle position is (2,4) , Width is 10 , Height is 20
2014-01-18 18:25:40.180 Rectangle The Perimeter is 60
2014-01-18 18:25:40.180 Rectangle The area is 200
2014-01-18 18:25:40.180 Rectangle The new position is (3,5)
2014-01-18 18:25:40.181 Rectangle SARectangle is dealloc
2014-01-18 18:25:40.181 Rectangle SAPoinr is dealloc
Program ended with exit code: 0


Sian 发表于 2014-1-19 16:41:49

本帖最后由 Sian 于 2014-1-19 16:44 编辑

该程序还存在内存管理问题:
在声明SAPoint对象时,没有将SAPoint进行内存管理
因此,在@property (assign) SAPoint * origin 时需要优化,最简单的方法在声明时使用retain属性
修改成:@property (retain)SAPoint * origin;
main.m中创建point后,将point被调用后,负责创建point的指针有责任将point释放,因此需要在
ARectangle * rectangle = ;后加上;

页: [1]
查看完整版本: 业务练习(矩形)