scala define variable
// immutable variable val msg = "Hello, world!" //Variable variables, you don't need to write the variable type, scala will automatically deduce it val msg2: java.lang.String = "Hello again, world!"
The basic structure of scala function
Define the function as follows
def max(x: Int, y: Int): Int = { if (x > y) x else y }
scan loop
var i = 0 while (i < args.length) { println(args(i)) i += 1 } //Use scala function syntax args.foreach(arg => println(arg)) //full parameter form args.foreach((arg: String) => println(arg)) //If the function text has only one parameter, it can be abbreviated in one sentence args.foreach(println) //Iterative, can be written in a script, such as test.scala for (arg <- args) println(arg)
Syntax of scala function literal
Parameterized array with type
val greetStrings = new Array[String](3) greetStrings(0) = "Hello" greetStrings(1) = ", " greetStrings(2) = "world!\n" for (i <- 0 to 2) print(greetStrings(i)) //The above paragraph is equivalent to val greetStrings:Array[String] = new Array[String](3) greetStrings.update(0, "Hello") greetStrings.update(1, ", ") greetStrings.update(2, "world!\n") for (i <- 0.to(2)) print(greetStrings.apply(i))
val numNames = Array("zero", "one", "two") //The above code is equivalent to val numNames2:Array[String] = Array.apply("zero", "one", "two")
1+2 equals
1.+(2)
object definition
package test.obj /** * Define the Rational class, the (n:Int,d:Int) following the class is the parameter passed in by the constructor */ class Rational(n:Int,d:Int) { //Define these two lines to be compared in the lessThan() function, no error will be reported when this.n == that.n val numer: Int = n val denom: Int = d private val g = gcd(n.abs, d.abs) println("create->"+n+"/"+d) //Check that the denominator cannot be 0, require(d != 0) /** * Define a secondary constructor, which calls the primary constructor from the constructor */ def this(n:Int) { this(n,1) } /** * Overload the toString function, the default result returned by toSring is Rational@4b1d6571 */ override def toString():String = { n +"/"+ d } /** * 两个Rational相加,这个操作不会改变原始值,会返回一个新对象 */ def add(that: Rational): Rational = { new Rational( number * that.denom + that.number * denom, denom * that.denom) } /** * 比较两个Rational大小,这里用到了this关键字,这里是可以省略的 */ def lessThan(that: Rational): Boolean = { this.number * that.denom < that.number * this.denom } /** * 返回两个Rational中最大的那个,注意else中的this就不能省略了,返回就返回空了 */ def max(that: Rational): Rational = { if(this.lessThan(that)) { that } else { this } } //定义私有的函数 private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b) /** * 重载 + ,这样可以实现两个Ratioinal的 + 操作了 */ def +(that: Rational): Rational = { new Rational( number * that.denom + that.number * denom, denom * that.denom ) } /** * 函数重载,可以传入整型,这样可以实现Rational和整数之间的运算 */ def +(i: Int): Rational = { new Rational(number + i * denom, denom) } /** * 重载 * 操作,让两个Rational之间实现乘法操作 */ def *(that: Rational): Rational = { new Rational(number * that.number, denom * that.denom) } /** * 重载 * 操作,让Rational和整数相乘 */ def *(i: Int): Rational = { new Rational(number * i, denom) } /** * 重载- 操作,让两个Rational之间实现减法操作 */ def -(that: Rational): Rational = { new Rational( number * that.denom - that.number * denom, denom * that.denom ) } /** * 重载 * 操作,让Rational和整数之间实现减法操作 */ def -(i: Int): Rational = { new Rational(number - i* denom, denom) } /** * 重载 * 操作,让两个Rational之间实现除法操作 */ def /(that: Rational): Rational = { new Rational(number * that.denom, denom * that.number) } /** * 重载 * 操作,让Rational和整数之间实现除法操作 */ def /(i: Int): Rational = { new Rational(number, denom * i) } } /** * 测试类 */ object Test { def main(args : Array[String]) { var x1 = new Rational(1,2) var x2 = new Rational(2,3) var x3 = x1.add(x2) println(x3) println(x1 + x3) println(x2 * x3) println("======") println(2 + x1) } implicit def intToRational(x: Int): Rational = { new Rational(x) } }