The notes are complex! 1. the singleton objects in Scala are decorated with objects, similar to static classes in C ++. When calling its internal functions, you can call them directly using the object name. you cannot apply for a new function! 2. a file can define classes and Singleton objects with the same name. a singleton object is called a CompanionObject of a class ). The two can access each other's private members. The companion object can be understood as a class with the same name.
The notes are complex!
1. the singleton objects in Scala are decorated with objects, similar to static classes in C ++. When calling its internal functions, you can call them directly using the object name. you cannot apply for a new function!
2. a file can define classes and Singleton objects with the same name. a singleton Object is called a Companion Object of a class ). The two can access each other's private members. Companion objects can be understood as static member functions of classes with the same name.
3. the Scala file name does not need to be the same as the class name. But it is best to be the same for convenient maintenance and programming.
4. when defining a variable, it must be modified with val or var:
var hw =new helloworld("Hi,my name is lming_08")
Scala will automatically identify the type as helloworld. of course, you can also specify the type.
var hw:helloworld =new helloworld("Hi,my name is lming_08")
5. define a method
scala> def max(x: Int, y: Int): Int = if (x < y) y else xmax: (Int,Int)Int
The Int value after the colon is the result type. if the defined method is recursive, the result type of the method must be explicitly specified. For a common method, the compiler can deduce the result type. Note: the parameter type must be explicitly specified. When the method body involves multiple statements, you need to use {} to enclose them.
6. circular usage
def arrLoop(arr:Array[String])={ arr(0)="abc" arr(1)="123" arr(2)="ABC" for(elem<- arr) println(elem+", hello") } def arrLoop2(arr:Array[String])={ arr(0)="+abc" arr(1)="+123" arr(2)="+ABC" arr.foreach(elem => println(elem)) } def arrLoop3(arr:Array[String])={ arr(0)="-abc" arr(1)="-123" arr(2)="-ABC" for(i <- 0 to arr.length -1){ println(arr(i)) } }
7. array differs from List. Array is a sequence of variable objects. After instantiation, the length of Array cannot be changed, but the value of its elements can be changed. List is a sequence of immutable objects, more functional-oriented; Scala List and Java List (java. util. list) is also different. Scala List is unchangeable, while Java List is variable. More importantly, Scala List is more functional-oriented. Val oneTwoThree = List (1, 2, 3)
Not complete...
References: http://www.artima.com/scalazine/articles/steps.html
Github exercise code