1.略了吧 步步高点读机
2.所有int类型改为float或double类型
3.略了吧 superclass和subclass的关系搞懂就OK!
4.
1 - (void) translate: (XYPoint *) tp 2 { 3 origin.x = tp.x; 4 origin.y = tp.y; 5 } 6 7 //main 8 Rectangle *myRect = [[Rectangle alloc] init]; 9 XYPoint *myPoint = [[XYPoint alloc] init]; 10 11 [myPoint setX:100 andY:200]; 12 [myRect setWidth:5 andHeight:8]; 13 14 // myRect.origin = myPoint; 15 [myRect setOrigin:myPoint]; 16 [myPoint setX:111 andY:222]; 17 [myRect translate:myPoint]; 18 19 NSLog(@"Rectangle w = %.1f, h = %.1f", [myRect width], [myRect height]); 20 NSLog(@"Origin at (%.1f, %.1f)", myRect.origin.x , myRect.origin.y ); 21 NSLog(@"Area = %.1f, perimeter = %.1f", [myRect area], [myRect perimeter]); 22
5. 子类Rectangle类没帖上来 没什么必要
//GraphicObject类 #import <Foundation/Foundation.h> @interface GraphicObject : NSObject @property int fillColor, lineColor; @property BOOL filled; - (void) setFillColor: (int) f andLineColor: (int) l andFilled: (BOOL) yesOr; @end #import "GraphicObject.h" @implementation GraphicObject @synthesize fillColor, lineColor, filled; - (void) setFillColor: (int) f andLineColor: (int) l andFilled: (BOOL) yesOr { if (yesOr == NO) { fillColor = f; lineColor = l; } } @end //Circle类 #import "GraphicObject.h" @interface Circle : GraphicObject @property double radius; - (double) area; - (double) perimeter; @end #import "Circle.h" @implementation Circle @synthesize radius; - (double) area { return 3.14 * radius * radius; } - (double) perimeter { return 2 * 3.14 * radius; } @end //Triangle类 #import "GraphicObject.h" @interface Triangle : GraphicObject @property double ab, bc, ac; - (void) setAB: (double) a andBC: (double) b andAC: (double) c; - (double) area; - (double) perimeter; @end #import "Triangle.h" @implementation Triangle @synthesize ab, bc, ac; - (void) setAB: (double) a andBC: (double) b andAC: (double) c { ab = a; bc = b; ac = c; } - (double) area { int s ; s = (ab + bc + ac) / 2; return sqrt(s * (s - ab) * (s - bc) * (s - ac)); } - (double) perimeter { return ab + ac +bc; } @end // main #import <Foundation/Foundation.h> #import "GraphicObject.h" #import "Circle.h" #import "Triangle.h" int main(int argc, const char * argv[]) { @autoreleasepool { //测试GraphicObject GraphicObject *newGraphic = [[GraphicObject alloc] init]; [newGraphic setFillColor:2 andLineColor:3 andFilled:NO]; NSLog(@"fillColor is %d, lineColor is %d",newGraphic.fillColor, newGraphic.lineColor); //测试Circle Circle *newCirle = [[Circle alloc] init]; [newCirle setRadius:4.0]; NSLog(@"Area %.2f, Perimeter %.2f", [newCirle area], [newCirle perimeter]); //测试Triangle Triangle * newTriangle = [[Triangle alloc] init]; [newTriangle setAB: 5 andBC: 6 andAC: 7]; NSLog(@"Area %.2f, Perimeter %.2f", [newTriangle area], [newTriangle perimeter]); } return 0; }
6.
1 - (BOOL) containsPoint: (XYPoint *) aPoint 2 { 3 if (aPoint.x >= origin.x && aPoint.x <= origin.x + width && 4 aPoint.y >= origin.y && aPoint.y <= origin.y + height ) { 5 return YES; 6 }else 7 return NO; 8 }
7. 先不做了 重叠的情况太多 回头再做
8.我这个考虑的可能没那么全面
1 - (void) darw 2 { 3 for (int i = 1; i <= width; i++) { 4 printf("_"); 5 } 6 printf("\n"); 7 for (int h = 1; h <= height; h++) { 8 printf("| |\n"); 9 } 10 for (int i = 1; i <= width; i++) { 11 printf("_"); 12 } 13 printf("\n"); 14 }