"虚拟"方法在目标c中的返回类型 [英] "virtual" method's return type in objective-c
问题描述
如果您能帮助我,我会很高兴,
Tnx!
看看这个:
#import< Foundation / Foundation.h>
@interface A:NSObject {}
- (A *)newItem;
- (void)hello;
@end
@interface B:A {int filler; }
- (B *)newItem;
- (void)hello;
- (void)foo;
@end
@implementation A
- (A *)newItem {NSLog(@A newItem);返回自我}
- (void)hello {NSLog(@hello from A);
@end
@implementation B
- (B *)newItem {NSLog(@B newItem);返回自我}
- (void)hello {NSLog(@hello from B:%d,filler); }
- (void)foo {NSLog(@foo!);
@end
int main(int argc,const char * argv [])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]在里面];
A * origA = [A new];
A * myA = [origA newItem];
NSLog(@myA:%@,myA);
B * origB = [B new];
B * myB = [origB newItem];
A * myBA = [origB newItem];
NSLog(@myB:%@ \\\
myBA:%@,myB,myBA);
[origA hello];
[origB hello];
[myA hello];
[myB hello];
[myBA hello];
NSLog(@Covariance?);
[pool drain];
return 0;
}
这是相当浓缩的语法,内存管理很糟糕,但你可以看到 newItem
是虚拟的(发送 newItem
到 myBA
返回一个 B
)和协变,这似乎是你想要的。
请注意,您还可以执行以下操作:
B * myAB = (B *)[origA newItem];
但是返回一个 A
,并发送 foo
会告诉你,该类没有响应选择器 #foo
。如果你省略了(B *)
cast,那么在编译时你会收到一个警告。
但是ISTM协调不是大问题,在Objective-C中。
I have a class which is supposed to be abstract. In one of it's abstract methods the return type may be an instance of class1,class2 or class3, depending on the class that's implementing the method. I'm wondering how should I declare the method in the abstract class. I thought about using dynamic typing, but I want the return type to be restricted to one of the 3 classes, not every type, and in addition I'm not sure I can override it so that in the inheriting class the return type will not match the return type in the abstract class.
I'd be glad if you could help me with this,
Tnx!
Take a look at this:
#import <Foundation/Foundation.h>
@interface A : NSObject { }
- (A*) newItem;
- (void) hello;
@end
@interface B : A { int filler; }
- (B*) newItem;
- (void) hello;
- (void) foo;
@end
@implementation A
- (A*) newItem { NSLog(@"A newItem"); return self; }
- (void) hello { NSLog(@"hello from A"); }
@end
@implementation B
- (B*) newItem { NSLog(@"B newItem"); return self; }
- (void) hello { NSLog(@"hello from B: %d", filler); }
- (void) foo { NSLog(@"foo!"); }
@end
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
A *origA = [A new];
A *myA = [origA newItem];
NSLog(@"myA: %@", myA);
B *origB = [B new];
B *myB = [origB newItem];
A *myBA = [origB newItem];
NSLog(@"myB: %@\nmyBA: %@", myB, myBA);
[origA hello];
[origB hello];
[myA hello];
[myB hello];
[myBA hello];
NSLog(@"Covariance?");
[pool drain];
return 0;
}
This is rather condensed syntax, and memory management sucks, but you can see that newItem
is virtual (sending newItem
to myBA
returns a B
) and covariant, which seems to be what you want.
Note that you could also do:
B *myAB = (B*)[origA newItem];
but that returns an A
, and sending foo
to it would tell you that the class does not respond to selector #foo
. If you omitted the (B*)
cast, you would get a warning about this at compile time.
But ISTM that covariance is no big problem, in Objective-C.
这篇关于"虚拟"方法在目标c中的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!