Objective-c选择器,其他语言如c++, python, ruby, java, javascript有类似的东

objective-c selector, do other languages such as c++, python, ruby, java, javascript have similar thing?

本文关键字:javascript java python 其他 选择器 语言 Objective-c c++ ruby      更新时间:2023-10-16
// 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有类似的东西吗?由于

是。c++有指向执行类似函数的成员的指针——这些指针可以识别一个实例方法,然后可以调用该实例方法,提供要调用该方法的对象和参数,类似的语义(模Objective-C使用动态绑定,c++使用延迟绑定),但语法与performSelector:withObject:等不同。

别人?