1. val和var的区别 val定义的是一个常量,无法改变其内容 scala> val s = 0 s: Int = 0 scala> s = 2 <console>:12: error: reassignment to val s = 2 ^ 如果要声明其值可变的变量,可以使用var scala> var s = 0 s: Int = 0 scala> s = 5 s: Int = 5 在scala中建议使用val,除非你真的想改变他的内容. 2. 基本类型 Byt
/* 学慕课网上<Scala程序设计>课程跟着敲的代码 作为代码参考也是很好的 在scala_ide.org上下载eclipse IDE,新建一个worksheet,就可以像在xcode的playground那样玩了, 就是如下所示,写变量直接在右边显示结果 链接:scala集合文档http://docs.scala-lang.org/zh-cn/overviews/collections/introduction 函数式编程,核心是状态不变 */ var x = 1 //> x : I
协变 案例一: class Animal {} class Bird extends Animal {} class Animal {} class Bird extends Animal {} //协变 class Covariant[T](t:T){} val cov = new Covariant[Bird](new Bird) val cov2:Covariant[Animal] = cov c不能赋值给c2,因为Covariant定义成不变类型. 稍微改一下: class Animal
package edu.snnu.test object list2 { //把字符串转化成一个char类型的list "99 Red Balloons".toList //> res0: List[Char] = List(9, 9, , R, e, d, , B, a, l, l, o, o, n, s) "99 Red Balloons".toList.filter(x => Character.isDigit(x)) //> res1: L