[关闭]
@fiy-fish 2015-05-18T15:02:57.000000Z 字数 942 阅读 1211

立方体练习

Objective-c


  1. // Cube.h
  2. // setter和getter
  3. //
  4. // Created by Aaron on 15/5/18.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. @interface Cube : NSObject
  9. {
  10. int _length;
  11. int _width;
  12. int _height;
  13. }
  14. -(void)setLength:(int)length;
  15. -(void)setWidth:(int)width;
  16. -(void)setHeight:(int)height;
  17. -(int)tiJi;
  18. @end
  1. // Cube.m
  2. // setter和getter
  3. //
  4. // Created by Aaron on 15/5/18.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import "Cube.h"
  8. @implementation Cube
  9. -(void)setLength:(int)length
  10. {
  11. _length = length;
  12. }
  13. -(void)setWidth:(int)width
  14. {
  15. _width = width;
  16. }
  17. -(void)setHeight:(int)height
  18. {
  19. _height = height;
  20. }
  21. -(int)tiJi
  22. {
  23. return _length*_width*_height;
  24. }
  25. @end
  1. // main.m
  2. // 立方体练习
  3. //
  4. // Created by Aaron on 15/5/18.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "Cube.h"
  9. /*
  10. 创建两个立方体对象,长宽高分别为4,5,6和7,8,9,求体积各是多少?
  11. */
  12. int main(int argc, const char * argv[])
  13. {
  14. @autoreleasepool {
  15. Cube *cub1 = [Cube alloc];
  16. [cub1 setLength:5];
  17. [cub1 setWidth:6];
  18. [cub1 setHeight:7];
  19. NSLog(@"%d",[cub1 tiJi]);
  20. }
  21. return 0;
  22. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注