通过在Dart中添加2个对象键,是否有更好的方法来创建对象?

问题描述

我制作了一个仅具有int属性的Stats类。我想创建一种添加两个Stats实例以创建一个新实例的方法。我所做的是以下内容

class Stats {
    final int movement;
    final int strength;
    final int constitution;

    Stats(
            {this.movement,this.strength,this.constitution});

    Stats add(Stats other) {
        return Stats(
            movement: this.movement + other.movement,strength: this.strength + other.strength,constitution: this.constitution + other.constitution);
    }
}

是否存在通过在键到键之间进行映射的巧妙方法

解决方法

否。

Dart对象的属性不是您可以抽象的东西,至少在不使用dart:mirrors的情况下(然后它实际上不再是 clever )。