簡體   English   中英

Objective-C選擇器,其他語言,例如C ++,Python,ruby,java,javascript也有類似的東西嗎?

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

// 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 ++具有指向執行類似功能的成員的指針-這些成員標識一個實例方法,然后可以調用該方法提供對象以調用該方法,並提供參數,相似的語義(使用動態綁定的Modular Objective-C和使用后期綁定的C ++),但是performSelector:withObject:語法完全不同。

其他? 所有的語言都有在網絡上可用的描述...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM