本文介绍了在目标C中是否可以重载函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在目标C中是否可以进行函数重载?
嗯,大多数程序员说不,
但这似乎是可能的,例如

Is function overloading possible in Objective C ?
Well,Most of the programmers says no,
But it looks like possible,
for example:

-(int)AddMethod:(int)X :(int)Y
{
    return X + Y;
}
-(int)AddMethod:(int)X
{
    return X;
}

呼叫第一个1写入[self AddMethod :3];
叫最后一个写[self AddMethod: 3 :4];

to call 1st one write [self AddMethod :3];
to call last one write [self AddMethod: 3 :4];

推荐答案

在Objective-C中,方法重载是不可能的.但是,您的示例实际上是有效的,因为您创建了两个具有不同选择器的不同方法:-AddMethod::AddMethod:.每个交错参数都有一个冒号.在其中也放一些文字是正常的-addMethodX:Y:,但您不必这样做.

Method overloading is not possible in Objective-C. However, your example actually will work because you have created two different methods with different selectors: -AddMethod:: and AddMethod:. There is a colon for each interleaved parameter. It's normal to put some text in also e.g. -addMethodX:Y: but you don't have to.

这篇关于在目标C中是否可以重载函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 07:54