1. 类
1.1. 类的定义
类的定义:
//定义Point类,构造器带有两个参数
class Point(var x: Int, var y: Int) {
//无返回值的类方法
def move(dx: Int, dy: Int): Unit = {
x = x + dx
y = y + dy
}
//没有参数但是返回值为String类型的重写方法
override def toString: String =
s"($x, $y)"
}
//创建类的实例
val point1 = new Point(2, 3)
point1.x // 2
println(point1) // prints (2, 3)
构造器可以带有默认值:
class Point(var x: Int = 0, var y: Int = 0){
...
}
val origin = new Point // x, y都取默认值0
val point1 = new Point(1)//x=1,y=0
println(point1.x) // prints 1
私有成员变量以及重新定义的Getter/Setter方法:
private var _x = 0
private var _y = 0
private val bound = 100
def x = _x
def x_= (newValue: Int): Unit = {
if (newValue < bound) _x = newValue else printWarning
}
def y = _y
def y_= (newValue: Int): Unit = {
if (newValue < bound) _y = newValue else printWarning
}
private def printWarning = println("WARNING: Out of bounds")
}
val point1 = new Point
point1.x = 99
point1.y = 101 // prints the warning
类定义中的其他细节:
//在Scala中,类并不用声明为public。
//Scala源文件中可以包含多个类,所有这些类都具有公有可见性。
class Person {
//用val修饰的变量是只读属性的,只带getter方法但没有setter方法
//(相当与Java中用final修饰的变量)
//字段必须初始化
val id = "1234"
//用var修饰的变量,默认同时有公开的getter方法和setter方法
var age: Int = 18
//类私有字段,有私有的getter方法和setter方法,只能在类的内部使用
private var name: String = "王老五"
//对象私有字段,访问权限更加严格的,Person类的方法只能访问到当前对象的字段
private[this] val hobby = "旅游"
}
scala中,在实现属性时你有如下四个选择:
(1) var foo: Scala自动合成一个getter和一个setter
(2) val foo: Scala自动合成一个getter
(3) 由你来定义foo和foo_=方法
(4) 由你来定义foo方法
1.2. 构造器
注意:
(1) 主构造器会执行类定义中的所有语句
(2) 主构造器如果有参数直接放置在类名之后
class ConstructorDemo ( val id: Int ) { … }
(3) 主构造器变成私有的,可以像这样放置private关键字:
class ConstructorDemo private ( val id: Int ) { … }
此时,用户必须通过辅助构造器来构造Person对象
class ConstructorDemo {
private var var1 = ""
private var var2 = 0
//辅助构造器1
def this(var1:String) {
this() //调用主构造器
this.var1 = var1
}
//辅助构造器2
def this(var1:String, var2:Int) {
this(var1) //调用辅助构造器1
this.var2 = var2
}
}
2. 特质
特质用来在类之间进行接口或者属性的共享。类和对象都可以继承特质,特质不能被实例化,因此也没有参数。
特质的定义:
trait Iterator[A] {
def hasNext: Boolean
def next(): A
}
使用特质定义其子类:
class IntIterator(to: Int) extends Iterator[Int] {
private var current = 0
override def hasNext: Boolean = current < to
override def next(): Int = {
if (hasNext) {
val t = current
current += 1
t
} else 0
}
}
val iterator = new IntIterator(10)
iterator.next() // returns 0
iterator.next() // returns 1
使用特质统一编程:
import scala.collection.mutable.ArrayBuffer
trait Pet {
val name: String
}
class Cat(val name: String) extends Pet
class Dog(val name: String) extends Pet
val dog = new Dog("Harry")
val cat = new Cat("Sally")
val animals = ArrayBuffer.empty[Pet]
animals.append(dog)
animals.append(cat)
animals.foreach(pet => println(pet.name)) // Prints Harry Sally
Mixins 用于进行类组合的特质:
abstract class A {
val message: String
}
class B extends A {
val message = "I'm an instance of class B"
}
//此处的特质C即为mixin
trait C extends A {
def loudMessage = message.toUpperCase()
}
class D extends B with C
val d = new D
println(d.message) // I'm an instance of class B
println(d.loudMessage) // I'M AN INSTANCE OF CLASS B
3. 抽象类
定义一个抽象类:
abstract class Person(val name: String){
//没有方法体,是抽象方法
def id: Int
}
class Employ(name:String) extends Person(name){
//实现,不需要overide关键字
def id = name.hashCode
}
定义带有抽象类型成员的特质:
trait Buffer {
type T
val element: T
}
定义一个抽象类,增加类型的上边界
abstract class SeqBuffer extends Buffer {
type U
type T <: Seq[U]
def length = element.length
}
abstract class IntSeqBuffer extends SeqBuffer {
type U = Int
}
abstract class IntSeqBuffer extends SeqBuffer {
type U = Int
}
//使用匿名类将 type T 设置为 List[Int]
def newIntSeqBuf(elem1: Int, elem2: Int): IntSeqBuffer =
new IntSeqBuffer {
type T = List[U]
val element = List(elem1, elem2)
}
val buf = newIntSeqBuf(7, 8)
println("length = " + buf.length)
println("content = " + buf.element)
本文为原创文章,如果对你有一点点的帮助,别忘了点赞哦!比心!如需转载,请注明出处,谢谢!