目录
- Scala类
- 创建类
- Scala类的访问级别
- Scala方法
- 方法参数类型
- 方法返回值
- 方法的 = 号
- 方法表达式
- 方法调用
- Scala中分号推断
- Scala操作符
- Scala的getter和setter
- 自定义
- getter和setter
- Bean
- Scala类构造器
- 主构造器
- 辅助构造器
- Scala嵌套类
Scala类
创建类
class Example{}
Scala类的访问级别
Scala类默认访问级别是Public,可以设置Private私有化。
// 只能在类内部访问
private class Example{}
// 类外部不能修改属性
class Example{
private var prop = 0
}
object Example {
def main(args: Array[String]): Unit = {
val ex = new Example
// 如,会报错
ex.prop = 1
}
}
Scala方法
方法参数类型
Scala方法中参数的类型是val类型(不可修改)
// 会报错Reassignment to val,val类型赋值错误
def sum(a: Int, b: Int): Int = {
a = a + b
a
}
方法返回值
// 直接输入返回值,省略return
def sum(a: Int, b: Int): Int = {
val sum = a + b
sum
}
方法的 = 号
Scala方法如果没有添加 = ,会默认返回值为Unit,也就是没有返回值。
// 返回值sum,没有=返回空
def sum(a: Int, b: Int): Int = {
val sum = a + b
sum
}
方法表达式
如果单行表达式时,{} 可以省略
def sum(a: Int, b: Int): Int = a + b
方法调用
如果定义方法时没有参数,调用时可以省略()。
class Example{
def hi() : Unit = {
println("Hi")
}
}
//或者写成 def hi = println("Hi"),那么不能通过括号.hi()调用
object Example {
def main(args: Array[String]): Unit = {
val ex = new Example
ex.hi
ex.hi()
}
}
Scala中分号推断
Scala语句多行表达式,句尾分号可省略,如果一行有多条语句,必须加分号。
// 可省略
var a = 0
var b = 0
// 必须加
var a = 0; var b = 0
Scala操作符
Scala操作符其实是方法,所以应该放在需要操作的属性后面
val a = 1
val b = 2
val z = a + b // 等同于 a.+ b
// 甚至换行
val z = a +
b
Scala的getter和setter
自定义
Scala中使用公共字段,可以直接改变,不具备安全性。
class Example{
var prop = 0
}
object Example {
def main(args: Array[String]): Unit = {
val ex = new Example
ex.prop = 1
}
}
getter和setter
可以自定义。
class Example{
var privateProp = 0
def prop = privateProp
def prop_=(newProp : Int){
privateProp = newProp
}
}
object Example {
def main(args: Array[String]): Unit = {
val ex = new Example
println(ex.prop) // 0
ex.prop = 2
println(ex.prop) // 2
}
}
Bean
类似于Java,将Scala字段标注为 @BeanProperty时,getter和setter方法会自动生成。
需要导入包
import scala.beans.BeanProperty
import scala.beans.BeanProperty
class Example {
@BeanProperty var prop : Int = _
}
object Example {
def main(args: Array[String]): Unit = {
val ex = new Example
ex.prop
println(ex.prop) // 0
ex.prop = 2
println(ex.prop) // 2
ex.getProp
println(ex.prop) // 2
ex.setProp(4)
println(ex.prop) // 4
}
}
由@BeanProperty会生成四个方法,分别对应上面几种测试。
name: String
name_= (newValue: String): Unit // 仅限于var
getName(): String
setName (newValue: String): Unit // 仅限于var
Scala类构造器
Scala主构造器通常和类是结合在一起的,这样比较简洁,相当于类像方法一样也接收参数。除了主构造器它还可以有多个辅助构造器。
主构造器
主构造器的参数直接放在类名之后。
// 私有属性,没有setter
class Example (val prop: String, val id: Int) {
...
}
object Example {
def main(args: Array[String]): Unit = {
val ex = new Example("hi", 2) //使用主构造器实例化对象
println(ex.prop + " : " + ex.id)
}
}
如果不带var、val ,属于对象私有,类似于private[this],只能通过方法调用。
class Example private (prop: String, id: Int) {
def des : String = prop + " " + id
}
object Example {
def main(args: Array[String]): Unit = {
val ex = new Example("hi", 2)
println(ex.des)
}
}
主构造器私有后,只能通过辅助构造器构建对象。
class Example private (val prop: String, val id: Int) {
...
}
辅助构造器
- 辅助构造器的名称为this
- 每一个辅助构造器都必须以一个对先前已定义的其他辅助构造器或主构造器的调用开始
object Example {
def main(args: Array[String]): Unit = {
val ex = new Example //主构造器
val ex2 = new Example("Hi") //第一个辅助构造器
val ex3 = new Example("Hello",18) //第二个辅助构造器
}
}
class Example {
private var prop = ""
private var id = 0
// 第一个辅助构造器
def this(prop: String) {
this() //调用主构造器
this.prop = prop
}
// 第二个辅助构造器
def this(prop: String, id: Int) {
this(prop) //调用前一个辅助构造器
this.id = id
}
}
Scala嵌套类
Scala中几乎可以在任何语法结构中内嵌其他任何语法构造。
class Person {
//在 Person类中定义 Man类
class Man(val name: String) {
...
}
private val mans = new ArrayBuffer[Man]
}
外部类的this.引用
class Person (val name: String) {
class Man (val name: String) {
def info = name + "inside" + Person.this.name
}
// 或者定义变量指向Person.this
// class Man (val name: String) { outer =>
// def info = name + "inside" + outer.name
// }
}