gpt4 book ai didi

reflection - Dart虚拟机不保证变量不能是变量但可能是函数?

转载 作者:行者123 更新时间:2023-12-03 03:28:43 26 4
gpt4 key购买 nike

无法理解变量如何成为函数。

示例代码。

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
================================

我在维基百科中找到了这个信息。
  • 在面向对象编程中,实例变量是定义在类中的变量(即成员变量)。
  • 在面向对象编程中,成员变量(有时称为成员字段)是与特定类相关联的变量,并且可以对其所有方法进行访问。

  • 我还问另一个问题。

    在面向对象的编程语言 Dart 中,实例变量(即成员变量)不是实例的成员吗?至少在“dart:mirrors”库的解释中。

    最佳答案

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

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

  • 我还找到了 final Map<Symbol, MethodMirror> instanceMembers

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

    关于reflection - Dart虚拟机不保证变量不能是变量但可能是函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20707762/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com