Val X:t Val X:t=e declares a constant
var x:t var x:t=e declares a variable
DEF ABC (X:T): t=e
def adder (m:t,n:t) =m+n
Numeric type:
Integer value: Int Long short Byte
Floating point type: double,float
Boolean value: Boolean (True/false)
Character Value: char,string
In addition to the string Java.lang others are attributed to the Scala package
Val one6= ' aaa ' char type is single quote
Val one6= "AAA" string type is double quotes
Scala does not have any operators, all operator operations are calls to methods (functions)
As 1+2 is actually 1 called. + () This method 1.+ (2)
for (I <-1 to 4)
println ("NO." +i)
1 to 4 (1=<i<=4); 1 until 4 (1=< i <=4-1)
Fixed-length array declaration
Val a=new array[t] (N) i.e.: Val a=new Array[int] (10)
Or
Val b= Array ("AA", "BB")
Variable-length array declaration
Val c=new Arraybuffer[t] ()
C+=e
c+= (E1,E2)
C++=array (E3,E4)
C.trimend (2) removing 2 elements from the end of the array
C.insert (2,E3,E4) inserts a e3,e4 after the second element
C.remove (3) Removing an element after the third element
C.remove (3,2) Two elements after removing the third element
D=c.toarray variable-length array to fixed-length array d
D=a.tobuffer fixed-length array to variable-length array D
can also Val G=b+array ("CC", "DD") Val g=b-array ("AA")
Enumerate array members with a For statement, add 1 to each array member and generate a new array
Val new=for (i<-c) yield i+1
Scala mapping
An array is a collection of elements that contain a set of key-value pairs
For non-mutable mappings
Val scores = Map ("Alice", "Bob", 33)//construct a immutable map[string,int]
Val Ss=scores + (A4->B4,A5->B5)//Add
Val Ss1=scores-(A4->B4,A5->B5)
To construct a mutable map:
Val treasuremap=map[int,string] ()//key is of type Int, value is String type
Val scores2 = Scala.collection.mutable.Map ("Alice", "Bob", 33)//construct a mutable map
Scores2 + = (A4->B4,A5->B5)
Scores2-=A4
Enumeration of mappings
For ((i,j) <-z) E (I,J)
Gets the key or value of the map
Val E=z.keyset
Val F=z.values
Ganso is a collection of different types of values
Val g= (1,1.2, "SSS")
Using method _1,_2,_3 to access elements of a meta-ancestor
Val h=g._1 or Val h=g _1 (space)
Class
Class hello{
Private Val value1= "Hello"
var value2= "World"
def add () {println (value1+value2)}
def plus (M:char) =value2+m
}
Declaring objects with new after class declarations
Val one=new Hello
Scala provides getter and setter methods for the fields in each class
Var declared fields have getter and setter methods (read and write)
Val declared field only getter method, no setter method
Each class has a primary constructor and is intertwined with the class definition
Such as
Class Hello (val va1:string,var va2:string) {
println ("Ssssss")
Val Value3=val+va2
}
Val two=new Hello ("Wlllcome", "Home")
Scala allows any syntactic structure to be nested in any syntax structure
Class a{Pointto =//pointto point to external Class A
var value2= "SSS"
Class b{
Val Values3=a.this.value2
var value4=pointto.value2
}
Wildcard "_" means any, "_*" means any length
}
Scala Object
The object syntax defines a single instance of a class
Associated objects
When a singleton object has the same name as a class, it is called a companion object
Scala features
Args.foreach (Arg=>println (ARG)) equivalent to for (arg<-args) println (ARG)
Package Com.persia
Object Funcreturntype {
To always return a value
def add (x:int,y:int): int={
X+y
}
Def returnunit (): unit={
println ("Another to return void")
}
Notation Two, omitting the non-unit return value, or, if there is no write return value, type deduction based on what follows the equal sign
def test (X:int) ={
X
}
Three, omit equal sign, return unit
Def returnvoid () {
println ("return void")
}
Four: Omit curly braces, if the function contains only one statement, then the curly braces can choose not to write
def max2 (x:int, y:int) = if (x > Y) x else y
def greet () = println ("Hello, world!")
def main (args:array[string]): Unit ={
println (ADD)
println (Test (1))
}
}
Scala Learning Notes