给定一个带有最终私有字段_bar的类:
class Foo() {
final Bar _bar;
Foo({Bar bar}) : _bar = bar {
//error: list cannot be used as setter, it is final
_bar = new Bar();
}
}
尝试在参数列表中进行设置会导致此错误
//error: default values of an object must be constant
Foo({Bar bar: new Bar()}) : _bar = bar ..
我想保留可选参数,以便可以在单元测试期间注入模拟。最好的方法是什么?
class Foo {
final Bar _bar;
Foo({Bar bar}) : _bar = bar != null ? bar : new Bar();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句