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

2024-10-05 10:46:20 发布

您现在位置:Python中文网/ 问答频道 /正文

// 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有类似的功能吗? 谢谢


Tags: theismainasthiscarsamesel
1条回答
网友
1楼 · 发布于 2024-10-05 10:46:20

是的。C++具有指向执行类似函数的成员的指针,这些实例标识了一个实例方法,该实例方法可以调用对象提供调用方法和参数,类似的语义(使用动态绑定和使用后期绑定的C++的Objo Objto-C),但不同的语法为^ {CD1>}等

其他人呢?所有的语言在网上都有描述。。。你知道吗

相关问题 更多 >

    热门问题