我是scala的初學者。在嘗試實施 Collections 時有疑問..
class MyLinkedHashMap[K, V] {
var initialCapacity = 16
private var index = 0
var map: Array[Node[K, V]]
var head: Node[K, V]
var tail: Node[K, V]
class Node[K, V] {
var key: K;
var value: V;
var next: Node[K, V]
var before: Node[K, V]
var after: Node[K, V]
def Node(key: K, value: V, next: Node[K, V]) {
this.key = key
this.value = value
this.next = next
}
}
def MyLinkedHashMap() {
map = new Array[Node[K, V]](initialCapacity)
}
def MyLinkedHashMap(initialCapacity: Int) {
if (initialCapacity < 0) {
throw new IllegalArgumentException();
}
map = Array[Node[K, V]](initialCapacity)
}
private def hash(key:K): Int = {
return Math.abs(key.hashCode()) % initialCapacity
}
def put(key: K, value: V): Unit = {
if (key == null) {
return;
}
var hashIndex = hash(key)
val newNode = new Node[K, V](key, value, null)
}
}
我在第 42 行得到的錯誤是
建構式節點的引數太多
但是,如果我在 MyLinkedHashMap 的物件中宣告類 Node 我不會收到此錯誤。我想知道這個錯誤的原因。
而且我不希望 Node 類是抽象的,但是當未宣告為抽象時會引發以下錯誤。
“Node”類必須宣告為抽象或在“MyLinkedHashMap.Node”中實作抽象成員“next: Node[K, V]”
為什么 Node 類必須是抽象的?
uj5u.com熱心網友回復:
我在第 42 行得到的錯誤是
建構式節點的引數太多
在這里,您Node使用三個引數呼叫 's 建構式:
new Node[K, V](key, value, null)
但是在這里你定義了Node沒有引數串列的類的主要建構式:
class Node[K, V]
并且也沒有定義輔助建構式。
因此,唯一存在的建構式Node是不帶引數但您使用三個引數呼叫它的建構式。
如果要呼叫帶有三個引數的建構式,則需要定義一個帶有三個引數的建構式(可能是主建構式),可能是這樣的:
class Node[K, V](var key: K, var value: V, var next: Node[K, V])
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/511723.html
標籤:斯卡拉仿制药收藏品
