objective-c选择器,做其他语言如c ++,python,ruby,java,javascript有类似的东西吗?

时间:2014-03-20 07:26:05

标签: java python c++ objective-c ruby

// main.m
#import <Foundation/Foundation.h>
#import "Car.h"

int main(int argc, const char * argv[]) {

@autoreleasepool {
    Car *porsche = [[Car alloc] init];
    porsche.model = @"Porsche 911 Carrera";

    SEL stepOne = NSSelectorFromString(@"startEngine");
    SEL stepTwo = @selector(driveForDistance:);
    SEL stepThree = @selector(turnByAngle:quickly:);

    // This is the same as:
    // [porsche startEngine];
    [porsche performSelector:stepOne];

    // This is the same as:
    // [porsche driveForDistance:[NSNumber numberWithDouble:5.7]];
    [porsche performSelector:stepTwo
                  withObject:[NSNumber numberWithDouble:5.7]];

    if ([porsche respondsToSelector:stepThree]) {
        // This is the same as:
        // [porsche turnByAngle:[NSNumber numberWithDouble:90.0]
        //              quickly:[NSNumber numberWithBool:YES]];
        [porsche performSelector:stepThree
                      withObject:[NSNumber numberWithDouble:90.0]
                      withObject:[NSNumber numberWithBool:YES]];
    }
    NSLog(@"Step one: %@", NSStringFromSelector(stepOne));
}
return 0;
}

对于objective-c选择器,其他语言如c ++,python,ruby,java,javascript有类似的东西吗? 感谢

1 个答案:

答案 0 :(得分:0)

是。 C ++具有指向执行类似功能的成员的指针 - 这些标识一个实例方法,然后可以调用该方法,提供调用方法的对象和参数,类似语义(使用动态绑定的模数Objective-C和使用后期绑定的C ++)但是performSelector:withObject:等语法相当不同。

其他人?所有语言都有网上描述......

相关问题