多个同名函数 [英] Multiple functions with the same name

查看:20
本文介绍了多个同名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Swift 的新手,我一直在浏览一些教程,其中许多教程不止一次定义了同名的函数.

I'm new to Swift and I've been going thru some tutorials and many of them define a function more than once with the same name.

我已经习惯了其他无法做到这一点的编程语言,否则会引发错误.

I'm used to other programming languages where this cannot be done otherwise it throws an error.

因此我查看了官方 Swift Manual 并检查了 override 关键字,看看我能从中得到什么,但我仍然无法理解以下代码:

Therefore I've checked the official Swift Manual and also checked the override keyword to see what I could get out of it, but still I cannot understand the following code:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")

    cell.textLabel?.text = "Row #\(indexPath.row)"
    cell.detailTextLabel?.text = "Subtitle #\(indexPath.row)"

    return cell
}

从我所看到的函数 tableView 设置在第 1 行和第 5 行,我注意到的唯一区别是第一个 tableView 函数返回一个 Int 和第二个返回一个 Object (UITableViewCell).

From what I can see the function tableView is set in line #1 as well as in line #5, the only difference I noticed is that the first tableView function returns an Int and the second one returns an Object (UITableViewCell).

在这种情况下,我从结果中看到第二个函数没有覆盖第一个函数.

In this case I see from the result the second function is NOT overriding the first one.

这是什么意思?为什么可以多次定义同名函数而不覆盖它?

What does this mean and why is it possible to define a function more than once with the same name without overriding it?

推荐答案

如果两个函数的类型不同,或者可以通过它们的外部参数参数标签来区分,则允许定义两个具有相同名称的函数.函数的Type由括号中的参数Types,后跟->,后跟返回Type组成.请注意,参数标签不是函数类型的一部分.(但请参阅下面的更新.)

You are allowed to define two functions with the same name if they have different Types, or if they can be distinguished by their external parameter argument labels. The Type of a function is composed of the parameter Types in parentheses, followed by ->, followed by the return Type. Note that the argument labels are NOT a part of the function's Type. (But see UPDATE below.)

例如,以下函数具有相同的名称并且属于类型 (Int, Int) ->整数:

For example, the following functions both have the same name and are of Type (Int, Int) -> Int:

// This:
func add(a: Int, b: Int) -> Int {
    return a + b
}

// Is the same Type as this:
func add(x: Int, y: Int) -> Int {
    return x + y
}

这将产生编译时错误 - 将标签从 a:b: 更改为 x:y: 不会区分这两个函数.(但请参阅下面的更新.)

This will produce a compile-time error - changing the labels from a:b: to x:y: does not distinguish the two functions. (But see UPDATE below.)

以 Web 先生的函数为例:

Using Mr. Web's functions as examples:

// Function A: This function has the Type (UITableView, Int) -> Int
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { ... }

// Function B: This function has the Type (UITableView, NSIndexPath) -> UITableViewCell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { ... }

// Function C: This made up function will produce a compile-time error because
// it has the same name and Type as Function A, (UITableView, Int) -> Int:
func tableView(arg1: UITableView, arg2: Int) -> Int { ... }

上面的函数A和函数B并不冲突,因为它们是不同的类型.上面的函数A和函数C确实冲突,因为它们具有相同的类型.如果类型保持不变,更改参数标签并不能解决冲突. (请参阅下面的更新.)

Function A and Function B above do not conflict because they are of different Types. Function A and Function C above do conflict because they have the same Type. Changing the parameter labels does not resolve the conflict if the Types remain the same. (See UPDATE below.)

override 是一个完全不同的概念,我认为其他一些答案涵盖了它,所以我会跳过它.

override is a different concept altogether, and I think some of the other answers cover it, so I'll skip it.

更新:我上面写的一些内容是不正确的.确实,函数的参数标签不是它的类型定义的一部分,但它们可以用来区分具有相同类型的两个函数,只要该函数具有不同的外部标签,以便编译器可以分辨出哪个您在调用时尝试调用的函数.示例:

UPDATE: Some of what I wrote above is incorrect. It is true that a function's parameter labels are not a part of it's Type definition, but they can be used to distinguish two functions that have the same Type, so long as the function has external labels that are different such that the compiler can tell which function you are trying to call when you invoke it. Example:

func add(a: Int, to b: Int) -> Int {    // called with add(1, to: 3)
    println("This is in the first function defintion.")
    return a + b
}

func add(a: Int, and b: Int) -> Int {   // called with add(1, and: 3)
    println("This is in the second function definition")
    return a + b
}

let answer1 = add(1, to: 3)    // prints "This is in the first function definition"
let answer2 = add(1, and: 3)   // prints "This is in the second function definition"

因此,在函数定义中使用外部标签将允许编译具有相同名称和相同类型的函数.因此,似乎您可以编写多个具有相同名称的函数,只要编译器可以通过它们的类型或外部签名标签来区分它们.我认为内部标签不重要.(但如果我错了,我希望有人能纠正我.)

So, the use of external labels in a function definition will allow functions with the same name and of the same type to compile. So, it appears that you can write multiple functions with the same name so long as the compiler can distinguish them by their types or by their external signature labels. I don't think internal labels matter. (But I hope someone will correct me if I'm wrong.)

这篇关于多个同名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆