当一个函数既要返回对象,又要返回null的时候,使用Option[] http://www.runoob.com/scala/scala-options.html Option是scala的选项,用来表示一个键是可选的(有值或者无值),比如判断一个map是否有值,可以直接使用get(xxx) ,返回的就是Option[String] Option[]有两个衍生值,一个是Some[],一个是None final case class Some[+A](x: A) extends Option[A]…
Option的解释: Represents optional values. Instances of Option are either an instance of scala.Some or the object None. Option[A] (sealed trait) 有两个取值: 1. Some[A] 有类型A的值 2. None 没有值 Option一般有两种用法: 1. 模式匹配 Option[A] option option match { case…
Alternatives Use null as a last resort. As already mentioned, Option replaces most usages of null. If you using null to implement deferred initialisation of a field with some expensive calculation, you should use a lazy val. Canonical initialisation…
看到Option类型就知道这本教材应该要说那个了. 使用过guava后,应该知道guava中的Optional类的作用是什么.算了找下原始文档好了: Optional<T> is a way of replacing a nullable T reference with a non-null value. An Optional may either contain a non-null T reference (in which case we say the reference is &…
样本类:添加了case的类便是样本类.这种修饰符可以让Scala编译器自动为这个类添加一些语法上的便捷设定. //样本类case class //层级包括一个抽象基类Expr和四个子类,每个代表一种表达式 //样本类自动添加与类名一致的工厂方法 abstract class Expr case class Var(name:String) extends Expr//括号内参数不用加val,默认为加val的字段 case class Number(num:Double) extends Expr…
Recently I transit to use scala to program. scala is a functional and objected oriented language, but it has seamless java Interoperability (they both run in JVM and freely mixed). Compared to the java that I am familiar to, there are some common co…
一. 类.对象.继承.特质 1.类 Scala的类与Java.C++的类比起来更简洁 定义: package com.jiangbei //在Scala中,类并不用声明为public. //Scala源文件中可以包含多个类,所有这些类都具有公有可见性. class Person { // 定义一个不可变的val(只有getter)和可变的var(getter setter都有) // 更直观的理解,可以通过反编译工具打开out目录的.class文件 val id = 9527 var name…