1

Cannot understand how variable can be a function.

Sample code.

import 'dart:mirrors';

class Foo {
 int baz;
}

void main() {
  var name = "baz=";
  var typeMirror = reflectClass(Foo);
  for(var member in typeMirror.instanceMembers.values) {
    print(MirrorSystem.getName(member.simpleName));
    if(MirrorSystem.getName(member.simpleName) == name) {
      if(member is MethodMirror) {
        print("================================");
        print("Info about METHOD $name");
        print("isSetter: ${member.isSetter}");
        print("isVariable: false, because it is a method");
        print("================================");
      }
    }
  }
}
==
hashCode
_identityHashCode
toString
noSuchMethod
runtimeType
_cid
_leftShiftWithMask32
baz
baz=
================================
Info about METHOD baz=
isSetter: true
isVariable: false, because it is a method
================================

I found this inforamtion in Wikipedia.

  • In object-oriented programming, an instance variable is a variable defined in a class (i.e. a member variable).

  • In object-oriented programming, a member variable (sometimes called a member field) is a variable that is associated with a specific class, and accessible for all its methods.

I also ask another question.

In object-oriented programming language Dart an instance variable (i.e. a member variable) is not a member of the instance? At least in the interpretation of "dart:mirrors" library.

4

1 回答 1

2

显然 getter 和 setter 方法是为公共字段自动生成的。

  • "baz=": isSetter == true
  • "baz": isGetter == true

我还找到了最终的 Map<Symbol, MethodMirror> instanceMembers

目的是捕获构成实例 API 的那些成员。因此不包括字段,但包括字段隐式引入的 getter 和 setter。

于 2013-12-20T16:31:05.490 回答