5

使用 Observable 扩展 Object 和将 Observable 应用于下面的类有什么区别。

运行应用程序时的结果是相同的。

library models;

import 'package:polymer/polymer.dart';

class Person extends Object with Observable {
  @observable String name;
  @observable bool signedAgreement = false;

  Person();

  Person.from(Person other) {
    name = other.name;
    signedAgreement = other.signedAgreement;
  }

  blank() {
    name = '';
    signedAgreement = false;
  }
}


library models;

import 'package:polymer/polymer.dart';

class Person extends Observable {
  @observable String name;
  @observable bool signedAgreement = false;

  Person();

  Person.from(Person other) {
    name = other.name;
    signedAgreement = other.signedAgreement;
  }

  blank() {
    name = '';
    signedAgreement = false;
  }
}
4

1 回答 1

8

这两个声明之间的行为没有区别。

这是弗洛里安·洛伊奇(Florian Loitsch )的一句话:

当您使用 mixin 扩展“Object”时,第一个 mixin 总是可以代替“Object”。

唯一的小区别在于类层次结构(superclass不一样):

import 'dart:mirrors';

abstract class Mixin {}
class A extends Mixin {}
class B extends Object with Mixin {}

main() {
  print(reflectClass(A).superclass); 
  // => ClassMirror on 'Mixin'

  print(reflectClass(A).superclass.superclass);
  // => ClassMirror on 'Object'

  print(reflectClass(B).superclass); 
  // => ClassMirror on 'dart.core.Object with .Mixin'

  print(reflectClass(B).superclass.superclass); 
  // => ClassMirror on 'Object'
}
于 2013-11-04T09:44:26.397 回答