首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从基类初始化属性?

问如何从基类初始化属性?
EN

Stack Overflow用户
提问于 2019-04-19 04:30:00
回答 2查看 192关注 0票数 0

我有许多扩展基类的类,并且在从默认构造函数中初始化的基类中获取成员时遇到了问题。

我已经尝试在默认构造函数中列出字段,使用和不使用'this.‘限定符。

我在没有设置其他类中的字段时也遇到过类似的问题,但通过放置‘that’纠正了这个问题。在田野前面。但是,如果我应用“the”。限定符添加到继承的字段中时,我收到一个错误,指出这些字段不在包含的类中。

基类:

代码语言:javascript
复制
import 'package:randomizer/model/includecondition.dart';
import 'package:randomizer/model/setup.dart';

abstract class Includable {
  List<IncludeCondition> includedWhen;
  List<IncludeCondition> includedIf;
  List<IncludeCondition> mustIncludeWhen;
  List<IncludeCondition> excludeWhen;

  Includable(
      {this.includedWhen,
      this.includedIf,
      this.mustIncludeWhen,
      this.excludeWhen});
}

派生类:

代码语言:javascript
复制
import 'package:randomizer/model/exclusiongroup.dart';
import 'package:randomizer/model/includable.dart';
import 'package:randomizer/model/includecondition.dart';
import 'package:randomizer/model/item.dart';

class Selection extends Includable {
  String name;
  int minNumToSelect;
  int maxNumToSelect;
  List<Item> items;
  List<ExclusionGroup> exclusionGroups;

  Selection(
      {this.name,
      this.items,
      this.exclusionGroups,
      this.minNumToSelect,
      this.maxNumToSelect,
      includedWhen,
      includedIf,
      mustIncludeWhen,
      excludeWhen});

  factory Selection.fromMap(Map<String, dynamic> map) {
    Selection result = Selection(
      name: map['name'] ?? '',
      minNumToSelect:
          map.keys.contains('minNumToSelect') ? map['minNumToSelect'] : 1,
      maxNumToSelect:
          map.keys.contains('maxNumToSelect') ? map['maxNumToSelect'] : 1,
      items: map.keys.contains('items') ? Item.fromList(map['items']) : null,
      exclusionGroups: map.keys.contains('exclusionGroups')
          ? ExclusionGroup.fromList(map['exclusionGroups'])
          : null,
      includedWhen: map.keys.contains('includedWhen')
          ? IncludeCondition.fromList(map['includedWhen'])
          : null,
      includedIf: map.keys.contains('includedIf')
          ? IncludeCondition.fromList(map['includedIf'])
          : null,
      mustIncludeWhen: map.keys.contains('mustIncludeWhen')
          ? IncludeCondition.fromList(map['mustIncludeWhen'])
          : null,
      excludeWhen: map.keys.contains('excludedWhen')
          ? IncludeCondition.fromList(map['excludedWhen'])
          : null,
    );

    return result;
  }
}

我希望能够在默认构造函数中设置所有这些字段。

EN

回答 2

Stack Overflow用户

发布于 2019-04-19 05:27:44

好吧,我相信你有问题的主要原因是你扩展了一个抽象类,而不是实现它。在Select类中将extends更改为implements。然后,您将会遇到错误,因为您的Select类需要拥有并覆盖抽象类中的所有属性。

代码:

代码语言:javascript
复制
class Selection implements Includable {
  String name;
  int minNumToSelect;
  int maxNumToSelect;
  List<Item> items;
  List<ExclusionGroup> exclusionGroups;

  @override
  List<IncludeCondition> excludeWhen;

  @override
  List<IncludeCondition> includedIf;

  @override
  List<IncludeCondition> mustIncludeWhen;

  @override
  List<IncludeCondition> excludeWhen;

  Selection(
      {this.name,
      this.items,
      this.exclusionGroups,
      this.minNumToSelect,
      this.maxNumToSelect,
      this.includedWhen,
      this.includedIf,
      this.mustIncludeWhen,
      this.excludeWhen});

}
票数 0
EN

Stack Overflow用户

发布于 2019-04-19 09:56:07

你忘了在继承的类中调用超级构造函数。这就是超类成员没有被初始化的原因。在Selection类构造函数中,您应该执行以下操作:

代码语言:javascript
复制
Selection(
      {this.name,
      this.items,
      this.exclusionGroups,
      this.minNumToSelect,
      this.maxNumToSelect,
      // specify the type  List<IncludeCondition> here is optional
      List<IncludeCondition> includedWhen,
      List<IncludeCondition> includedIf,
      List<IncludeCondition> mustIncludeWhen,
      List<IncludeCondition> excludeWhen}) : super( 
           includedWhen: includedWhen,
           includedIf : includedIf
           mustIncludeWhen : mustIncludeWhen,
           excludeWhen : excludeWhen
      ) ;

在: super( ... )行中,我们调用超类构造函数,然后初始化超类成员。您可以在其他继承的类中执行相同的操作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55753335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档