摘要:Traits as Interfaces Let’s start with something familiar. A Scala trait can work exactly like a Java interface. For example: A subclass can provide an
阅读全文
摘要:Reading Lines To read all lines from a file, call the getLines method on a scala.io.Source object: The result is an iterator (see Chapter 13). You can
阅读全文
摘要:In this chapter, we only discuss the case in which a class inherits from another class. See Chapter 10 for inheriting traits —the Scala concept that g
阅读全文
摘要:Packages A package can be defined in multiple files. For example and Multiple packages can be in a single file. For example Note: There is no enforced
阅读全文
摘要:Use the object construct when you need a class with a single instance, or when you want to find a home for miscellaneous values or functions. Singleto
阅读全文
摘要:Simple Classes and Parameterless Methods In Scala, a class is not declared as public . A Scala source file can contain multiple classes, and all of th
阅读全文
摘要:Constructing a Map The value of "Alice" -> 10 is ("Alice", 10). You could have equally well defined the map as Accessing Map Values To check whether t
阅读全文
摘要:Fixed-Length Arrays Variable-Length Arrays: Array Buffers Traversing Arrays and Array Buffers The variable i goes from 0 to a.length - 1 . To visit ev
阅读全文
摘要:An expression has a value; a statement carries out an action. Conditional Expressions: IF-ELSE In Scala, an if / else has a value. For example: if (x
阅读全文
摘要:Interpreter Values and Variables You are encouraged to use a val unless you really need to change the contents. The type of a variable or function is
阅读全文