gpt4 book ai didi

dart - Dart 中的函数重载

转载 作者:行者123 更新时间:2023-12-03 12:34:50 24 4
gpt4 key购买 nike

以下代码:

class Tools {
static int roll(int min, int max) {
// IMPLEMENTATION
}

static int roll(List<int> pair) {
// IMPLEMENTATION
}
}

呈现 The name 'roll' is already defined第二个错误 roll功能。

怎么来的?既然函数的参数是不同的,难道不应该应用多态吗?

编辑。更正标题以更好地反射(reflect)主题。

最佳答案

您的代码展示的是函数重载,与多态无关。

Dart 根本不支持函数重载。

您可以为方法或可选的命名或未命名参数使用不同的名称

// optional unnamed
void foo(int a, [String b]);
...
foo(5);
foo(5, 'bar');

// optional named
void foo(int a, {String b});
...
foo(5);
foo(5, b :'bar');

可选参数也可以有默认值。
可选的命名和未命名参数不能一起使用(单个函数只能使用一个或另一个)

多态和静态方法:

静态方法只能在没有类名作为前缀的情况下从定义它们的类内部访问。从子类调用时,需要使用父类(super class)的名称作为前缀。

关于dart - Dart 中的函数重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49734608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com