gpt4 book ai didi

dart - 你能让一个函数接受两种不同的数据类型吗?

转载 作者:行者123 更新时间:2023-12-03 02:46:16 25 4
gpt4 key购买 nike

我有一个函数应该接受两种不同的数据类型作为输入:

vec3 add(vec3 vec){
this.x += vec.x;
this.y += vec.y;
this.z += vec.z;

return this;
}
vec3 add(num scalar){
this.x += scalar;
this.y += scalar;
this.z += scalar;

return this;
}

但这会返回一个错误:

The name 'add' is already defined



有没有办法在 Dart 中完成这项工作?
我知道类型是可选的,但我想知道是否有办法。

最佳答案

Dart 不允许函数/方法重载。您可以为方法或可选或命名的可选参数使用不同的名称,以便能够使用具有不同参数集的方法。

关于dart - 你能让一个函数接受两种不同的数据类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32423409/

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