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