"虚拟"方法在目标c中的返回类型 [英] "virtual" method's return type in objective-c

查看:220
本文介绍了"虚拟"方法在目标c中的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应该是抽象的课。在其中一个抽象方法中,返回类型可能是class1,class2或class3的实例,具体取决于实现该方法的类。我想知道如何在抽象类中声明该方法。我想到使用动态打字,但是我希望返回类型被限制在3个类中的一个,而不是每个类型,另外我不知道我可以覆盖它,以便在继承类中返回类型不会匹配抽象类中的返回类型。



如果您能帮助我,我会很高兴,

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.

这篇关于&QUOT;虚拟&QUOT;方法在目标c中的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆