1.检测不到reduce方法,因为Complex类中没有定义;
2.合法。因为id类型可以用来存储属于任何类的对象(不能为id变量使用点运算符)
3.
1 //XYPoint类print方法 2 3 4 - (void) print 5 { 6 NSLog(@" (%g , %g) ", x, y); 7 } 8 9 10 //main函数部分 11 12 id dataValue; 13 14 XYPoint *x1 = [[XYPoint alloc] init]; 15 16 [x1 setX:12.01 andY:13]; 17 dataValue = x1; 18 [dataValue print];
4.
Fraction类
1 #import <Foundation/Foundation.h> 2 3 @interface Fraction : NSObject 4 @property int numerator, denominator; 5 6 - (void) print; 7 //- (void) print: (BOOL) isReduce; 8 - (void) setTo: (int) n over: (int) d; 9 //- (Fraction *) add: (Fraction *) f; 10 - (id) addFrac: (id) f; 11 //- (Fraction *) subtract: (Fraction *) f; 12 //- (Fraction *) multiply: (Fraction *) f; 13 //- (Fraction *) divide: (Fraction *) f; 14 - (void) reduce; //约简 15 - ( double) convertToNum; //转换为双精度值 16 17 @end 18 19 20 #import "Fraction.h" 21 22 @implementation Fraction 23 @synthesize numerator, denominator; 24 25 //@synthesize numerator,denominator; 26 27 - (void) print 28 { 29 NSLog(@"%d / %d", numerator, denominator); 30 } 31 32 //- (void) print: (BOOL) isReduce //BOOL参数 是否约简 33 //{ 34 // if (numerator % denominator == 0) { 35 // NSLog(@"%d",numerator/denominator); 36 // }else if (numerator > denominator){ 37 // [self reduce]; 38 // NSLog(@"%d %d/%d",numerator/denominator,numerator % denominator,denominator); 39 // }else if (isReduce) { 40 // [self reduce]; 41 // NSLog(@"%d/%d",numerator,denominator); 42 // }else 43 // NSLog(@"%d/%d",numerator,denominator); 44 // 45 //} 46 47 - (void) setTo: (int) n over: (int) d 48 { 49 numerator = n; 50 denominator = d; 51 } 52 53 - (id) addFrac: (id) f 54 { 55 Fraction *result = [[Fraction alloc] init]; 56 57 result.numerator = numerator * [f denominator] + denominator * [f numerator]; 58 result.denominator = denominator * [f denominator]; 59 60 return result; 61 } 62 63 //- (Fraction *) add: (Fraction *) f 64 //{ 65 // Fraction *result = [[Fraction alloc] init]; 66 // 67 // result.numerator = numerator * f.denominator + denominator * f.numerator; 68 // result.denominator = denominator * f.denominator; 69 // 70 // [result reduce]; 71 // return result; 72 //} 73 // 74 //- (Fraction *) subtract: (Fraction *) f 75 //{ 76 // Fraction *result = [[Fraction alloc] init]; 77 // 78 // result.numerator = numerator *f.denominator - denominator *f.numerator; 79 // result.denominator = denominator *f.denominator; 80 // 81 // [result reduce]; 82 // return result; 83 //} 84 // 85 //- (Fraction *) multiply: (Fraction *) f 86 //{ 87 // Fraction *result = [[Fraction alloc] init]; 88 // 89 // result.numerator = numerator *f.numerator; 90 // result.denominator = denominator *f.denominator; 91 // 92 // [result reduce]; 93 // return result; 94 //} 95 // 96 //- (Fraction *) divide: (Fraction *) f 97 //{ 98 // Fraction *result = [[Fraction alloc] init]; 99 // 100 // result.numerator = numerator *f.denominator; 101 // result.denominator = denominator *f.numerator; 102 // 103 // [result reduce]; 104 // return result; 105 //} 106 107 - (void) reduce 108 { 109 int u = numerator; 110 int v = denominator; 111 int temp; 112 113 while (v != 0) { 114 temp = u % v; 115 u = v; 116 v = temp; 117 } 118 119 numerator /= u; 120 denominator /= u; 121 } 122 123 - ( double) convertToNum 124 { 125 if (denominator != 0) { 126 return (double) numerator / denominator; 127 }else 128 return NAN; 129 130 } 131 132 @end
Complex类
1 #import <Foundation/Foundation.h> 2 3 @interface Complex : NSObject 4 5 @property double real, imaginary; 6 7 - (void) print; 8 9 - (void) setReal: (double) a andImaginary: (double) b; 10 11 //- (Complex *) add: (Complex *) f; 12 - (id) addComp: (id) c; 13 14 @end 15 16 17 #import "Complex.h" 18 19 @implementation Complex 20 21 @synthesize real, imaginary; 22 23 - (void) print 24 { 25 NSLog(@"%.1f + %.1fi", real, imaginary); 26 } 27 28 - (void) setReal: (double) a andImaginary: (double) b 29 { 30 real = a; 31 imaginary = b; 32 } 33 34 //- (Complex *) add: (Complex *) f 35 //{ 36 // Complex *result = [[Complex alloc] init]; 37 // 38 // result.real = real + f.real; 39 // result.imaginary = imaginary + f.imaginary; 40 // 41 // return result; 42 //} 43 44 - (id) addComp: (id) c 45 { 46 Complex *result = [[Complex alloc] init]; 47 48 result.real = real + [c real]; 49 result.imaginary = imaginary + [c imaginary]; 50 51 return result; 52 } 53 54 @end
main函数
1 #import <Foundation/Foundation.h> 2 #import "Fraction.h" 3 #import "Complex.h" 4 5 int main(int argc, const char * argv[]) { 6 @autoreleasepool { 7 8 id dataValue1, dataValue2, result; 9 10 Fraction *f1 = [[Fraction alloc] init]; 11 Fraction *f2 = [[Fraction alloc] init]; 12 13 [f1 setTo: 1 over: 2]; 14 [f2 setTo: 1 over: 3]; 15 16 dataValue1 = f1; 17 dataValue2 = f2; 18 result = [dataValue1 addFrac:dataValue2]; 19 [result print]; 20 21 //------------------------------------------------------ 22 Complex *c1 = [[Complex alloc] init]; 23 Complex *c2 = [[Complex alloc] init]; 24 25 [c1 setReal:12 andImaginary: 3]; 26 [c2 setReal: 4 andImaginary: 5]; 27 28 dataValue1 = c1; 29 dataValue2 = c2; 30 result = [dataValue1 addComp:dataValue2]; 31 [result print]; 32 33 } 34 return 0; 35 }
5. isMemberOfClass: 测试的是类中的直接成员关系。所以 第一条和第二条 中的类和对象没有直接成员关系;
1 Fraction *fraction = [[Fraction alloc] init]; 2 Complex *complex = [[Complex alloc] init]; 3 id number = [[Complex alloc] init]; 4 5 if ( [fraction isMemberOfClass:[Complex class]] == YES ) { 6 NSLog(@"1"); 7 } 8 if ( [complex isMemberOfClass:[NSObject class]] == YES) { 9 NSLog(@"2"); 10 } 11 if ( [complex isKindOfClass:[NSObject class]] == YES ) { 12 NSLog(@"3"); 13 } 14 if ( [fraction isKindOfClass:[fraction class]] == YES ) { 15 NSLog(@"4"); 16 } 17 if ([fraction respondsToSelector:@selector(print)] == YES ) { 18 NSLog(@"5"); 19 } 20 if ( [complex respondsToSelector:@selector(print)] == YES ) { 21 NSLog(@"6"); 22 } 23 if ( [Fraction instancesRespondToSelector:@selector(print)] == YES ) { 24 NSLog(@"7"); 25 } 26 if ( [number respondsToSelector:@selector(print)] == YES ) { 27 NSLog(@"8"); 28 } 29 if ( [number isKindOfClass:[Complex class]] == YES ) { 30 NSLog(@"9"); 31 } 32 if ( [[number class] respondsToSelector:@selector(alloc)] == YES ) { 33 NSLog(@"10"); 34 } 35