Scala learning (5)

There are only 3 expressions that don't return a value in scala

One is while and do-while . While in scala is called a loop, not an expression. It's because they don't produce meaningful results.

There is also a finally , if finally produces a result, it will be executed at the end of the function according to the characteristics of finally, then the result in try and catch will be meaningless. Only the result in finally will always be returned. For example, def returnTest = try{1 }finally{2}, then returnTest can only be 2. All this is different from java. In scala, finally does not return anything and is only used for resource release.

In scala, any assignment statement will not return the value of the assigned variable. It will only return a "()".

var line = ""
while((line = readLine()) != ""){
   println("Read : " + line)
}

This is the idiomatic way to read lines of text in java, c, and c++, but not in scala. Because line = readLine() always returns "()", () != "" is never equal.

scala> val f = (_:Int) + (_:Int)
f: (Int,Int) => Int = <function>

scala> f(5,10)
res11: Int = 15

Note that _ + _ will expand to a function literal with two arguments. This also explains why you can use this short form only if each argument appears at most once in the function literal. Multiple underscores refer to Multiple parameters, not the reuse of a single parameter. The first underscore represents the first parameter, the second underscore represents the second, the third, ........, and so on.

Define a no-argument method: use parentheses if the function you're calling performs an action, but omit parentheses if it only provides access to a property.

Extending classes: If you omit the extends clause, the Scala compiler will implicitly extend scala.AnyRef, which by default inherits from java.lang.Object

Abstract methods in java must specify abstract but not in scala.

Java has 4 namespaces (fields, methods, types and packages), scala has only 2 namespaces: value (including fields, methods, packages and singleton objects), type (including class and trait names)

scala's Any is the base class of any class

Traits and classes are out of 3 points. Everything else is the same as classes.

The main points are as follows:

  1. Traits cannot have class parameters
  2. A class in Scala can only inherit from one superclass and can extend any number of traits
  3. Traits can require the classes implementing them to have specific fields, methods, and superclasses
  4. Unlike Java interfaces, Scala traits can provide implementations of methods and fields
  5. When stacking multiple traits, order matters
  6. The super in the class is statically bound, and the super in the trait is dynamically bound.

Traits: Ordering , roughly speaking, the trait closer to the right comes into play first. When you call a method on a class with a mixin, the method on the rightmost trait is called first. If that method calls super, it calls methods of its left-hand trait, and so on.

import scala.collection.mutable.ArrayBuffer

abstract class IntQueue {
  def get(): Int

  def put(x: Int)
}

trait Incrementing extends IntQueue {
  abstract override def put(x: Int) = super.put(x + 1)
}

trait Filtering extends IntQueue {
  abstract override def put(x: Int) = if (x >= 0) super.put(x)
}

class BasicIntQueue extends IntQueue {
  private val buf = new ArrayBuffer[Int]()

  override def get(): Int = buf.remove(0)

  override def put(x: Int) {
    buf += x
  }
}

object BasicIntQueue {
  def main(args: Array[String]): Unit = {
    val queue = new BasicIntQueue with Filtering with Incrementing
    queue.put(10)
    queue.put(-1)
    println(queue.get())
    println(queue.get())
  }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325813978&siteId=291194637