简体   繁体   English

Dart 在工厂命名构造函数中初始化超级构造函数

[英]Dart Initialise Super Constructor in Factory Named Constructor

How do I initialise the variables of a super class in a named factory constructor?如何在命名工厂构造函数中初始化超级 class 的变量? Here is my sample code:这是我的示例代码:

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
    );
  }


} 

I am not sure how or where exactly do i call the super constructor for Class A so that I can initialise its variables.我不确定我如何或在哪里调用 Class A 的超级构造函数,以便我可以初始化它的变量。

Here is the way to call super :这是调用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
    );
  }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM