Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我在 scala 中有一个 MyList 类,其中一个列表作为私有成员。是否可以为我的类定义“()”以在给出正索引的情况下返回预期的内容,并在负索引的情况下从结尾开始(就像在 python 中一样)?
这可以通过以下apply方法完成:
apply
class PythonicArray { private val underlying = Array(1,2,3,4) def apply(n: Int) = { val i = if (n < 0) (underlying.length + n) else n underlying(i) } }