我有一个函数应该接受两种不同的数据类型作为输入:
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/