//main方法声明
def main(args: Array[String]): Unit = {
}
Any:所有其他类的超类,是抽象类
AnyRef:所有引用类的基类,与java中Object表示同一个意思
Nothing:所有类的子类
变量声明
var:声明可以变化的变量
val:声明不能变化的常量
//val声明的常量A地址不能修改,但是常量A中var修饰的属性值可以修改
class A {
var name = new String("name");
}
def main(args: Array[String]): Unit = {
val a = new A;
a.name = new String("123")
}
访问修饰符
private:外部类不能访问内部类私有修饰的成员
protected:只有子类能访问被保护成员,同一个包内不能访问被保护的成员
sealed:当前抽象类在子类都明确的情况下,为了防止继承滥用,为抽象类添加sealed
循环语句
object Test {
def main(args: Array[String]) {
var a = 0;
val numList = List(1,2,3,4,5,6,7,8,9,10);
// for 循环 结尾的yield a :将符合的结果存储到retVal中
var retVal = for{ a <- numList
if a != 3; if a < 8
}yield a
// 输出返回值
for( a <- retVal){
println( "aValue of a: " + a );
}
//直接使用to 或者until关键字 to是[] until是[)
for( a <- 1 to 3){
println( "Value of a: " + a );
}
}
}
/*
输出
aValue of a: 1
aValue of a: 2
aValue of a: 4
aValue of a: 5
aValue of a: 6
aValue of a: 7
value of a: 1
value of a: 2
value of a: 3
*/
方法与函数
函数可以作为方法的参数传递,并进行运算
object Test {
class A {
//定义一个方法 方法参数为 函数f:参数为两个int返回值为int, int a, int b
def method(f:(Int,Int)=>Int, a:Int, b:Int): Unit = {
print("调用方法结果为:"+f(a,b))
}
//方法调用
def main(args: Array[String]): Unit = {
val a = new A;
//定义函数 求两个数值的和
val function = (x:Int,y:Int) => x+y;
a.method(function,1,2);
}
}
数组
数组声明方式
var z = new Array[String](3)
var z = Array("A", "B", "C")
var array = new Array[String](3);
array(0) = "A";
array(1) = "B";
array(2) = "C";
//array = null;
for( a <- array) {
println(a)
}
类和对象
class类与object伴生对象
scala中没有static的概念,main方法需要写在伴生对象里面才能被运行。
伴生对象不用new也不能new
伴生对象中声明的变量和java中的静态成员变量一样。
伴生对象中声明的方法能通过类名直接调用。
伴生对象对于所有的class来说只有一份,单例。
代码示例:
//伴生对象Test
object Test {
//声明类A
class A {
//A类具有方法method
def method(a:Int) : Unit= {
println(a*2)
}
}
//B类继承A类
class B() extends A {
//声明成员变量a b
private var a = 0;
private var b = 0;
//构造方法 this括号中为参数 必须在第一行手动调用this()
def this(a :Int, b: Int) = {
this ()
this.a = a
this.b = b
}
//重写A父类方法method
override def method(a: Int): Unit = super.method(a)
//定义B类自己的方法methodB
def methodB(): Unit = {
println("成员变量a : "+a)
}
}
//main方法入口
def main(args: Array[String]): Unit = {
val bb = new B(1,2)
bb.methodB()
}
}
集合
集合分为不可变immutable与可变mutable两种
Option
option是一个定义了泛型的抽象类
sealed abstract class Option[+A] extends Product with Serializable
final case class Some[+A](x: A) extends Option[A]
case object None extends Option[Nothing]
代码demo
val myMap: Map[String, String] = Map("key1" -> "value")
val value1: Option[String] = myMap.get("key1")
val value2: Option[String] = myMap.get("key2")
println(value1) // Some("value1")
println(value2) // None
immutable(不可变)
类型
说明
immutable.List
存储可重复元素,有序
immutable.Set
存储不重复元素,无序
immutable.Map
存储键值对、无序
immutable.Queue
队列,先进先出,有序
immutable.Stack
栈,先进后出、有序
代码demo
val list = List("a","b","c")
val set = Set("a","b","c")
val map = Map(1 -> "a", 2 -> "b")
val queue = scala.collection.immutable.Queue[String]("a","b","c")
val stack = new scala.collection.immutable.Stack[String]()
mutable(可变)
类型
说明
ArrayBuffer
数组缓冲,底层链表
ListBuffer
列表缓冲,底层数组
StringBuilder
字符串缓冲
mutable.Queue
队列
mutable.Stack
栈
mutable.HashMap
键值对
代码demo
val arrayBuffer = new ArrayBuffer[String]()
arrayBuffer.insert(0,"a")
arrayBuffer += "b"
arrayBuffer += "c"
arrayBuffer -= "a"
arrayBuffer.remove(1)
println(arrayBuffer) //ArrayBuffer(b)
val listBuffer = new ListBuffer[String]()
listBuffer += "a"
listBuffer.insert(0,"b")
listBuffer.remove(1)
println(listBuffer) //ListBuffer(b)
val builder = new mutable.StringBuilder()
builder.append("a")
builder.insert(0,"b")
builder.deleteCharAt(0)
println(builder) //a
val queue = new mutable.Queue[String]()
queue += "a";
queue.enqueue("b")
queue.enqueue("c")
println(queue.dequeue()) //a
println(queue) //Queue(b, c)
val stack = new mutable.Stack[String]()
stack.push("a")
stack.push("b")
stack.push("c")
println(stack.pop()) //c
println(stack) //Stack(b, a)
val map = HashMap.empty[String, String]
map.put("1","a")
map.put("2","b")
println(map.remove("4")) //None
println(map.getOrElse("3","c")) //c
println(map) //Map(2 -> b, 1 -> a)