1

我有一堂课

    class Account extends Stuff{
      String name;
      newObject(){
        return new Account();
      }
    }

在 Stuff 类里面我有一个方法

    //generates list of objects of the same type 
    //as given object and fills attribute 
    generateObjectsFromExisting(names)
      {
        List list = new List();
        InstanceMirror instanceMirror = reflect(this);
        Symbol formatSymbol = new Symbol("newObject");
        for(var name in names){
          //calles newObject function from this and returns a new object
          var newInstanceObject = instanceMirror.invoke(formatSymbol, []);
          Symbol symbol = new Symbol("name");
          InstanceMirror field = newInstanceObject.setField(symbol,name);
          list.add(newInstanceObject.reflectee)
        }
        return list;
      }

所以写的时候

    main(){
      var account = new Account();
      List accounts = new List();
      accounts = account.generateObjectsFromExisting(['tim','tom']);
      print(account.name) // returns null
      print(accounts[0].name) // returns tim
      print(accounts[1].name) // returns tom
    }

这种方式的问题是

1 'generateObjectsFromExisting()' 在 'account' 对象上,而不是在 Account 上

2 我必须手动将“newObject”方法添加到我实现的每个类中。

我更喜欢像'Account.generateObjectsFromExisting()'这样的静态方法,但是如何访问'this'(因为它在静态中不可用)所以我可以说“this.new()”或相当于“new Account() ;" 例如“新这个();” 因此只能在 Stuff 中拥有一个“newObject”函数,或者可能根本不需要它。

所以现在我的代码看起来像这样

    class Account extends Stuff{
      String name;
    }

在东西

    static generateObjectsFromExisting(names)
      {
        List list = new List();
        for(var name in names){
          var object = new this();
          object.name = name;
          list.add(object)
        }
        return list;
      }

主要是

    main(){
      // returns list of Accounts filled with names
      accounts = Account.generateObjectsFromExisting(['tim','tom']);
      print(accounts[0].name) // returns tim
      print(accounts[1].name) // returns tom
    }

如果你能告诉我一种访问类的方法来做这样的事情 this.new(); 或新的 this(); 那么显然需要访问“帐户”类而不是扩展的“东西”

如果无法使用“this”方法,那么也许您可以向我展示一种如何从现有对象中访问 Class 的方法

喜欢

    generateObjectsFromExisting(names)
      {
        List list = new List();
        var class = this.class;
        var newObject = class.new():
        ...          
      }

还是我目前的方法是唯一的解决方案。.. 希望不是 :)

谢谢你

4

1 回答 1

2

There are two ways I can think of at the moment. But both of them are pretty close to your initial solution as they both use reflection..

The non-static solution:

class Stuff {

  generateObjectsFromExisting(List<String> names) {
    var cm = reflectClass(this.runtimeType);
    return names.map((name) {
      var newInstance = cm.newInstance(const Symbol(''), []).reflectee;
      newInstance.name = name;
      return newInstance;
    }).toList();
  }
}

The static solution:

class Stuff {

  static generateObjectsFromExisting(type, List<String> names) {
    var cm = reflectClass(type);
    return names.map((name) {
      var newInstance = cm.newInstance(const Symbol(''), []).reflectee;
      newInstance.name = name;
      return newInstance;
    }).toList();
  }
}

You would call the static solution like this:

var accounts = Stuff.generateObjectsFromExisting(Account, ['tim', 'tom']);

There might be another solution involving factory constructors but can't think of any right now. Also, this code would easily break when you get another subclass of Stuff that does not have a name attribute. I don't know if you really intended on putting that attribute on Account instead of Stuff.

Also answering you 'Class'-Question. There is no class in Dart, there is only the Type and to get it you can do:

Type type1 = Account;
Type type2 = account.runtimeType;

But the Type doesn't have any methods you could use to create a new instance.

于 2013-11-12T20:27:38.160 回答