方法声明vs Objecti中的Python默认值

2024-06-28 20:15:32 发布

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

For all intents and purposes, an Objective-C method declaration is simply a C function that prepends two additional parameters (see “Messaging” in the Objective-C Runtime Programming Guide ). Thus, the structure of an Objective-C method declaration differs from the structure of a method that uses named or keyword parameters in a language like Python, as the following Python example illustrates: In this Python example, Thing and NeatMode might be omitted or might have different values when called.

def func(a, b, NeatMode=SuperNeat, Thing=DefaultThing):
    pass

在Objective-c相关的书上展示这个例子的目的是什么?你知道吗


Tags: orandoftheinanthatexample
3条回答

我认为这里的重点是区分如何“使用”接收函数中的参数和objective-c如何。通常情况下:

public void accumulate(double value, double value1) {                                    

}

在目标c中:

-(void)accumulateDouble:(double)aDouble withAnotherDouble:(double)anotherDouble{


}

这是一个(糟糕的)例子,说明Objective-C不支持其他语言(例如Python)可能支持的某些特性。本文解释了Objective-C具有格式的“命名参数”

- (void)myMethodWithArgument:(NSObject *)argument andArgument:(NSObject *)another;

这些参数不支持默认值,Python就是这样做的。你知道吗

前面提到的两个参数暗示了Objective-C中的消息传递是如何在引擎盖下工作的,即在每个方法前面加上一个receiver对象和一个选择器。用Objective-C编写代码不需要知道这些细节,尤其是在初学者阶段,但是Apple解释了这个过程here。你知道吗

def func(a, b, NeatMode=SuperNeat, Thing=DefaultThing):
    pass

NeatMode,Thing是可选的命名参数 在目标c中,它们是

- (void) func:(int)a :(int)b NeatMode:(object*)SuperNeat Thing:(object*)DefaultThing

请阅读更多关于这个主题的文章 http://www.diveintopython.net/power_of_introspection/optional_arguments.html

相关问题 更多 >