年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12481|回复: 1

业务练习(矩形)

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-1-18 18:29:17 | 显示全部楼层 |阅读模式
    本帖最后由 Sian 于 2014-1-18 18:31 编辑

    需求分析:
    写一个Rectangle类,具备以下属性:位置(origin)、长(width)、宽(height)
    要求提供:初始化方法、工厂方法、业务方法(移动位置、求周长、求面积)
    程序实现:说明:我们程序实现中写两个类SAPoint与SARectangle,SAPoint专为矩形提供位置服务(矩形的一个属性)
    SAPoint.h
    1. //
    2. //  SAPoint.h
    3. //  Rectangle
    4. //
    5. //  Created by yusian on 14-1-18.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. @interface SAPoint : NSObject
    10. @property int x;
    11. @property int y;
    12. - (id) init;
    13. - (id) initWithX:(int)x andY:(int)y;
    14. + (id) pointWithX:(int)x andY:(int)y;
    15. - (void) show;
    16. @end
    复制代码
    SAPoint.m
    1. //
    2. //  SAPoint.m
    3. //  Rectangle
    4. //
    5. //  Created by yusian on 14-1-18.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "SAPoint.h"
    9. @implementation SAPoint
    10. - (id) init
    11. {
    12.     if (self = [super init]) {
    13.         _x = 1;
    14.         _y = 1;
    15.     }
    16.     return self;
    17. }
    18. - (id) initWithX:(int)x andY:(int)y
    19. {
    20.     if (self = [super init]) {
    21.         self.x = x;
    22.         self.y = y;
    23.     }
    24.     return self;
    25. }
    26. + (id) pointWithX:(int)x andY:(int)y
    27. {
    28.     return [[SAPoint alloc] initWithX:(int)x andY:(int)y];
    29. }
    30. - (void) show{
    31.     NSLog(@"The position is (%d,%d)",_x,_y);
    32. }
    33. - (void) dealloc
    34. {
    35.     NSLog(@"SAPoinr is dealloc");
    36.     [super dealloc];
    37. }
    38. @end
    复制代码
    SARectangle.h
    1. //
    2. //  SARectangle.h
    3. //  Rectangle
    4. //
    5. //  Created by yusian on 14-1-18.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. #import "SAPoint.h"
    10. @interface SARectangle : NSObject
    11. @property int width;
    12. @property int height;
    13. @property (assign) SAPoint *origin;
    14. - (id) initWithPoint:(SAPoint *)origin andWidth:(int)width andHeight:(int)height;
    15. + (id) rectangleWithPoint:(SAPoint *)origin andWith:(int)width andHeight:(int)height;
    16. - (void) show;
    17. - (void) moveToX:(int)x andY:(int)y;
    18. - (int) perimeter;
    19. - (void) perimeterPrint;
    20. - (int) area;
    21. - (void) areaPrint;
    22. @end
    复制代码
    SARectangle.m
    1. //
    2. //  SARectangle.m
    3. //  Rectangle
    4. //
    5. //  Created by yusian on 14-1-18.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "SARectangle.h"
    9. @implementation SARectangle
    10. - (id) initWithPoint:(SAPoint *)point andWidth:(int)width andHeight:(int)height
    11. {
    12.     self = [super init];
    13.     if (self) {
    14.         self.origin = point;
    15.         self.width = width;
    16.         self.height = height;
    17.     }
    18.     return self;
    19. }
    20. + (id) rectangleWithPoint:(SAPoint *)point andWith:(int)width andHeight:(int)height
    21. {
    22.     return [[SARectangle alloc] initWithPoint:(SAPoint *)point andWidth:(int)width andHeight:(int)height];
    23. }
    24. - (void) moveToX:(int)x andY:(int)y
    25. {
    26.     _origin.x = x;
    27.     _origin.y = y;
    28.     NSLog(@"The new position is (%d,%d)",_origin.x,_origin.y);
    29. }
    30. - (int) perimeter
    31. {
    32.     return (self.width+self.height)*2;
    33. }
    34. - (void) perimeterPrint
    35. {
    36.     NSLog(@"The Perimeter is %d",self.perimeter);
    37. }
    38. - (int) area
    39. {
    40.     return self.width*self.height;
    41. }
    42. - (void) areaPrint
    43. {
    44.     NSLog(@"The area is %d",self.area);
    45. }
    46. - (void) show
    47. {
    48.     NSLog(@"The rectangle position is (%d,%d) , Width is %d , Height is %d",_origin.x,_origin.y,self.width,self.height);
    49. }
    50. - (void) dealloc
    51. {
    52.     NSLog(@"SARectangle is dealloc");
    53.     [_origin release];
    54.     [super dealloc];
    55. }
    56. @end
    复制代码
    main.m
    1. //
    2. //  main.m
    3. //  Rectangle
    4. //
    5. //  Created by yusian on 14-1-18.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import <Foundation/Foundation.h>
    9. #import "SAPoint.h"
    10. #import "SARectangle.h"
    11. int main(int argc, const char * argv[])
    12. {
    13.    
    14.     @autoreleasepool {
    15.         
    16.         SAPoint * point = [SAPoint pointWithX:2 andY:4];//工厂方法
    17.         [point show];
    18.         
    19.         SARectangle * rectangle = [SARectangle rectangleWithPoint:point andWith:10 andHeight:20];
    20.         
    21.         [rectangle show];
    22.         [rectangle perimeterPrint];
    23.         [rectangle areaPrint];
    24.         [rectangle moveToX:3 andY:5];
    25.         [rectangle release];
    26.         
    27.     }
    28.     return 0;
    29. }
    复制代码
    输出结果:
    1. 2014-01-18 18:25:40.178 Rectangle[6671:303] The position is (2,4)
    2. 2014-01-18 18:25:40.179 Rectangle[6671:303] The rectangle position is (2,4) , Width is 10 , Height is 20
    3. 2014-01-18 18:25:40.180 Rectangle[6671:303] The Perimeter is 60
    4. 2014-01-18 18:25:40.180 Rectangle[6671:303] The area is 200
    5. 2014-01-18 18:25:40.180 Rectangle[6671:303] The new position is (3,5)
    6. 2014-01-18 18:25:40.181 Rectangle[6671:303] SARectangle is dealloc
    7. 2014-01-18 18:25:40.181 Rectangle[6671:303] SAPoinr is dealloc
    8. Program ended with exit code: 0
    复制代码


  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

     楼主| 发表于 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 = [SARectangle rectangleWithPoint:point andWith:10 andHeight:20];后加上[point release];

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-2 14:04 , Processed in 0.048013 second(s), 18 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表