如果我創建一個Map這樣的
Map<String, dynamic> map = Map<String, dynamic>();
在檔案中它說,它回傳LinkedHashMap
/// Creates an empty [LinkedHashMap].
///
/// This constructor is equivalent to the non-const map literal `<K,V>{}`.
///
/// A `LinkedHashMap` requires the keys to implement compatible
/// `operator==` and `hashCode`.
/// It iterates in key insertion order.
external factory Map();
而且LinkedHashMap也是一個抽象類,我們不能在 dart 中為抽象類創建物件。
abstract class LinkedHashMap<K, V> implements Map<K, V> {
/// Creates an insertion-ordered hash-table based [Map].
///
我們在Map建構式中得到的實際實體是什么?我正在使用dart 版本 2.16.1 最新穩定版
uj5u.com熱心網友回復:
Map默認建構式是使用其默認建構式的工廠構造函式LinkedHashMap。默認構造LinkedHashMap函式也是工廠建構式,它使物件成為幾個內部類之一:
_InternalLinkedHashMap_CompactLinkedIdentityHashMap_CompactLinkedCustomHashMap
至于選擇哪一個,那要看提供了什么樣的equals, hashCode,isValidKey方法。對于你打電話的情況Map(),它將是第一個,_InternalLinkedHashMap。
源代碼
這是我提到的類和建構式的最新版本(截至今天):
- 建構式:hash_factories.dart
_InternalLinkedHashMap: compact_hash.dart_CompactLinkedIdentityHashMap: compact_hash.dart_CompactLinkedCustomHashMap: compact_hash.dart
uj5u.com熱心網友回復:
我們在 Map Constructor 中得到的實際實體是什么?
答案是 LinkedHashMap。這是一個具體的類,而不是一個抽象的類。
請參閱LinkedHashMap。
的另一個例子implementers of Map是HashMap。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427322.html
