flutter - 如何在飞镖中创建多个构造函数?
问题描述
我想通过调用具有不同数量参数的构造函数来创建不同的对象。我怎样才能在 Dart 中实现这一点?
class A{
String b,c,d;
A(this.b,this.c)
A(this.b,this.c,this.d)
}
解决方案
基本上 Dart 不支持方法/构造函数重载。然而 Dart 允许命名构造函数和可选参数。
在您的情况下,您可以:
class A{
String b,c,d;
/// with d optional
A(this.b, this.c, [this.d]);
/// named constructor with only b and c
A.c1(this.b, this.c);
/// named constructor with b c and d
A.c2(this.b, this.c, this.d);
}
推荐阅读
- c# - 在打开另一个对话框之前关闭模态对话框(多线程)
- vue.js - Vue - 无法将特定值传递给更高的组件
- android - onNewIntent() 某些 Android 设备上的 TypeCastException
- bazel - Bazel 动作图中间人助记符在动作中
- yosys - 了解比特流 .bin 文件中 CRAM 位的组织
- javascript - 在 laravel 刀片中添加 javascript 的 CDN 链接
- github - 创建新的独立存储库时触发 GitHub 工作流
- r - 将样本数据映射到实际的 csv 数据
- windows-10 - 修改压缩的 zip 文件夹上下文菜单
- javascript - 在 HTML nagular js 上执行搜索