声明一个变量
声明一个常量
显式指定类型
定义一个main函数
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { println("hello scala") } }
定义一个普通函数
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { println(hello("scala")) } def hello(name : String) : String = { "hello " + name } }
函数不带参数时可以省略括号
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { println(hello()) } def hello() : String = { "hello scala" } }
定义一个匿名函数
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { println(add(1,3)) } def add = (x : Int,y : Int) => x + y }
把函数赋给一个常量
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { println(add(1,3)) } val add = (x : Int,y : Int) => x + y }
柯里化
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { println(add(1)(2)) } def add(x : Int)(y : Int) = x + y }
可变参数
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { println(variable("hello","admln")) } def variable(s : String*) = { s.foreach(x => println(x)) } }
参数默认值
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { println(theDefault()) println(theDefault("scala")) } def theDefault(s : String="admln") : String= { "hello " + s } }
判断表达式
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { val num = 5; val result = if(num > 0) 1 else 0 println(result) } }
while循环表达式
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { var (n,r) = (10,0) while(n>0) { r = r + n n = n - 1 } println(r) } }
for循环表达式
package org.admln.scala class HelloScala { } object HelloScala { def main (args: Array[String]) { //1<=10 for(i <- 1 to 10) { println(i) } //1<10 for(i <- 1 until 10) { println(i) } //只输出偶数 for(i <- 1 to 10 if i % 2 == 0 ) { println(i) } } }
创建一个类
package org.admln.scala class HelloScala { } class Person { var name : String = _ val age = 24 } object MyPerson { def main(args: Array[String]) { val p = new Person p.name = "admln" println(p.name + " " + p.age) } }
使用私有属性(外部不能访问)
package org.admln.scala class HelloScala { } class Person { var name : String = _ val age = 24 private[this] val gender = "male" } object MyPerson { def main(args: Array[String]) { val p = new Person p.name = "admln" println(p.name + " " + p.age) } }
构造器
package org.admln.scala class HelloScala { } class Person(var name : String,val age : Int){ println("main constructor") var gender : String = _ def this(name : String,age : Int,gender : String) { this(name,age) this.gender = gender } } object MyPerson { def main(args: Array[String]) { val p = new Person("admln",24,"male") println(p.name + " " + p.age) } }
继承
package org.admln.scala class HelloScala { } class Person(var name : String,val age : Int){ println("main constructor") var gender : String = _ def this(name : String,age : Int,gender : String) { this(name,age) this.gender = gender } } class Student(name : String,age : Int,val major : String) extends Person(name,age) { println("subclass is person,major is" + major) } object MyPerson { def main(args: Array[String]) { val s = new Student("admln",24,"spark") } }
覆写父类的方法和字段
package org.admln.scala class HelloScala { } class Person(var name : String,val age : Int){ println("main constructor") var gender : String = "mail" def this(name : String,age : Int,gender : String) { this(name,age) this.gender = gender } val school = "usa" } class Student(name : String,age : Int,val major : String) extends Person(name,age) { println("subclass is person,major is" + major) override def toString = "override method" override val school = "china" } object MyPerson { def main(args: Array[String]) { val s = new Student("admln",24,"spark") println(s.toString) println(s.gender) } }
抽象类
package org.admln.scala class HelloScala { } abstract class Person { def speak var name : String val age : Int } class Worker extends Person { def speak { println("speak") } var name = "admln" val age = 24 } object HelloScala extends App { val worker = new Worker worker.speak println(worker.name + " " + worker.age) }
App是trait的子类,内部实现了main方法并管理我们写的代码
trait
时间: 2024-10-11 21:23:14