如何在命名工厂构造函数中初始化超类的变量?这是我的示例代码:
class ClassA{
final a;
final b;
ClassA({this.a, this.b});
}
class ClassB extends ClassA{
final c;
List <String> myList;
ClassB({this.c,this.myList});
factory ClassB.fromJson(json){
var list = json["list"] as List;
List<String> tempList = [];
list.forEach((item)=>tempList.add(item));
return ClassB(
c: json["c"],
myList: tempList
);
}
}
我不确定如何或在哪里确切地调用A类的超级构造函数,以便初始化其变量。
答案 0 :(得分:1)
以下是致电super
的方法:
class ClassA{
final a;
final b;
ClassA({this.a, this.b});
}
class ClassB extends ClassA{
final c;
List <String> myList;
ClassB({final a, final b, this.c,this.myList}) : super(a: a, b: b);
factory ClassB.fromJson(json){
var list = json["list"] as List;
List<String> tempList = [];
list.forEach((item)=>tempList.add(item));
return ClassB(
c: json["c"],
myList: tempList
);
}
}