804
技術社區[雲棲]
obj-c編程05:類的多態與id動態綁定
說實在的,寫這第5篇的時候十分糾結,代碼老是不能動態綁定,在編譯時就會出錯,最後發現這是開了ARC的原因。開了ARC obj-c就不能動態綁定了嗎?這個問題還不清楚哦。把ARC關閉後,雖然會有警告,但運行是正確的。下麵上代碼:
#import <Foundation/Foundation.h>
@interface A:NSObject{
double i;
}
@property double i;
-(double)mul:(double)x;
-(void)show;
@end
@implementation A
@synthesize i;
-(double)mul:(double)x{
return i * x;
}
-(void)show{
NSLog(@"[A obj]i : %f",i);
}
@end
@interface B:NSObject{
int i;
}
@property int i;
-(int)mul:(int)x;
-(void)show;
@end
@implementation B
@synthesize i;
-(int)mul:(int)x{
return i * x;
}
-(void)show{
NSLog(@"[B obj]i : %d",i);
}
@end
int main(int argc,char *argv[])
{
@autoreleasepool{
NSLog(@"hello obj-c!");
//A *a = [[A alloc] init];
//id obj = a;
id obj = [[A alloc] init];
[obj setI:123.123];
//[obj mul:123];
[obj show];
NSLog(@"%f * %f is %f",[obj i],99.99,[obj mul:99.99]);
}
return 0;
}
apple@kissAir: objc_src$clang -fobjc-arc -framework Foundation 2.m -o 2
2.m:52:3: error: multiple methods named 'setI:' found with mismatched result,
parameter type or attributes
[obj setI:123.123];
^~~~~~~~~~~~~~~~~~
2.m:6:19: note: one possibility
@property double i;
^
2.m:26:16: note: also found
@property int i;
^
2.m:55:26: error: multiple methods named 'i' found with mismatched result,
parameter type or attributes
NSLog(@"%f * %f is %f",[obj i],99.99,[obj mul:99.99]);
^~~~~~~
2.m:6:19: note: one possibility
@property double i;
^
2.m:26:16: note: also found
@property int i;
^
2.m:55:40: error: multiple methods named 'mul:' found with mismatched result,
parameter type or attributes
NSLog(@"%f * %f is %f",[obj i],99.99,[obj mul:99.99]);
^~~~~~~~~~~~~~~
2.m:7:2: note: one possibility
-(double)mul:(double)x;
^~~~~~~~~~~~~~~~~~~~~~~
2.m:28:2: note: also found
-(int)mul:(int)x;
^~~~~~~~~~~~~~~~~
3 errors generated.
這是關了ARC之後的編譯和運行:
apple@kissAir: objc_src$clang -framework Foundation 2.m -o 2
2.m:52:3: warning: multiple methods named 'setI:' found
[obj setI:123.123];
^~~~~~~~~~~~~~~~~~
2.m:6:19: note: using
@property double i;
^
2.m:26:16: note: also found
@property int i;
^
2.m:55:26: warning: multiple methods named 'i' found
NSLog(@"%f * %f is %f",[obj i],99.99,[obj mul:99.99]);
^~~~~~~
2.m:6:19: note: using
@property double i;
^
2.m:26:16: note: also found
@property int i;
^
2.m:55:40: warning: multiple methods named 'mul:' found
NSLog(@"%f * %f is %f",[obj i],99.99,[obj mul:99.99]);
^~~~~~~~~~~~~~~
2.m:7:2: note: using
-(double)mul:(double)x;
^~~~~~~~~~~~~~~~~~~~~~~
2.m:28:2: note: also found
-(int)mul:(int)x;
^~~~~~~~~~~~~~~~~
3 warnings generated.
apple@kissAir: objc_src$./2
2014-06-30 08:51:28.972 2[828:507] hello obj-c!
2014-06-30 08:51:28.974 2[828:507] [A obj]i : 123.123000
2014-06-30 08:51:28.974 2[828:507] 123.123000 * 99.990000 is 12311.068770
這個問題還沒得到解決,希望哪位高人給予解答一下,在這之前我先跳過往後學鳥
,畢竟時間不等笨貓啊!
[2014-07-05 第1次新增]:多態的第二個例子
我們來看看一個簡單的代碼,其正確實現了對象的多態功能,雖然編譯時會有警告:
int main(int argc, char *argv[]){
@autoreleasepool {
msg(@"Hello World!");
NSString *str = @"hello apple";
NSObject *obj = str;
id item = str;
msg(@"%lu %lu",[obj length],[item length]);
}
return 0;
}
編譯運行如下:
wisy@wisy-ThinkPad-X61:~/src/objc_src$ clang -objc-arc -O3 -g0 $OBJC_OPTS -lobjc -lgnustep-base -o 1 1.m
1.m:12:23: warning: 'NSObject' may not respond to 'length'
msg(@"%lu %lu",[obj length],[item length]);
~~~ ^
1.m:3:24: note: expanded from macro 'msg'
#define msg(...) NSLog(__VA_ARGS__)
^
1 warning generated.
wisy@wisy-ThinkPad-X61:~/src/objc_src$ ./1
2014-07-05 09:45:58.092 1[3309] Hello World!
2014-07-05 09:45:58.095 1[3309] 11 11
前麵一個問題答案貌似也清楚了,類B沒有從A繼承啊!你從NSObject繼承,當運行時動態綁定的對象自然不知道到底是調用A還是B對象的方法鳥.
最後更新:2017-04-03 06:03:10