一、定义一个简单的类
class HelloWorld {
private var name = "leo"
def sayHello() { print("Hello, " + name) }
def getName = name
}
val helloWorld = new HelloWorld
helloWorld.sayHello()
print(helloWorld.getName)
二、getter与setter
class Student {
var name = "leo"
}
val leo = new Student
print(leo.name)
leo.name = "leo1"
三、 自定义getter与setter
class Student {
private var myName = "leo"
def name = "your name is " + myName
def name_=(newValue: String) {
print("you cannot edit your name!!!")
}
}
val leo = new Student
print(leo.name)
leo.name = "leo1"
四、仅暴露field的getter方法
class Student {
private var myName = "leo"
def updateName(newName: String) {
if(newName == "leo1") myName = newName
else print("not accept this new name!!!")
}
def name = "your name is " + myName
}
五、private[this]的使用
class Student {
private var myAge = 0
def age_=(newValue: Int) {
if (newValue > 0) myAge = newValue
else print("illegal age!")
}
def age = myAge
def older(s: Student) = {
myAge > s.myAge
}
}
六、Java风格的getter和setter方法
import scala.reflect.BeanProperty
class Student {
@BeanProperty var name: String = _
}
class Student(@BeanProperty var name: String)
val s = new Student
s.setName("leo")
s.getName()
七、 辅助constructor
class Student {
private var name = ""
private var age = 0
def this(name: String) {
this()
this.name = name
}
def this(name: String, age: Int) {
this(name)
this.age = age
}
}
八、主constructor
class Student(val name: String, val age: Int) {
println("your name is " + name + ", your age is " + age)
}
class Student(val name: String = "leo", val age: Int = 30) {
println("your name is " + name + ", your age is " + age)
}
九、 内部类
import scala.collection.mutable.ArrayBuffer
class Class {
class Student(val name: String) {}
val students = new ArrayBuffer[Student]
def getStudent(name: String) = {
new Student(name)
}
}
val c1 = new Class
val s1 = c1.getStudent("leo")
c1.students += s1
val c2 = new Class
val s2 = c2.getStudent("leo")
c1.students += s2
extends
class Person {
private var name = "leo"
def getName = name
}
class Student extends Person {
private var score = "A"
def getScore = score
}
override和super
class Person {
private var name = "leo"
def getName = name
}
class Student extends Person {
private var score = "A"
def getScore = score
override def getName = "Hi, I'm " + super.getName
}
override field
class Person {
val name: String = "Person"
def age: Int = 0
}
class Student extends Person {
override val name: String = "leo"
override val age: Int = 30
}
isInstanceOf和asInstanceOf
class Person
class Student extends Person
val p: Person = new Student
var s: Student = null
if (p.isInstanceOf[Student]) s = p.asInstanceOf[Student]
getClass和classOf
class Person
class Student extends Person
val p: Person = new Student
p.isInstanceOf[Person]
p.getClass == classOf[Person]
p.getClass == classOf[Student]
使用模式匹配进行类型判断
class Person
class Student extends Person
val p: Person = new Student
p match {
case per: Person => println("it's Person's object")
case _ => println("unknown type")
}
protected
class Person {
protected var name: String = "leo"
protected[this] var hobby: String = "game"
}
class Student extends Person {
def sayHello = println("Hello, " + name)
def makeFriends(s: Student) {
println("my hobby is " + hobby + ", your hobby is " + s.hobby)
}
}
调用父类的constructor
class Person(val name: String, val age: Int)
class Student(name: String, age: Int, var score: Double) extends Person(name, age) {
def this(name: String) {
this(name, 0, 0)
}
def this(age: Int) {
this("leo", age, 0)
}
}
匿名内部类
class Person(protected val name: String) {
def sayHello = "Hello, I'm " + name
}
val p = new Person("leo") {
override def sayHello = "Hi, I'm " + name
}
def greeting(p: Person { def sayHello: String }) {
println(p.sayHello)
}
抽象类
abstract class Person(val name: String) {
def sayHello: Unit
}
class Student(name: String) extends Person(name) {
def sayHello: Unit = println("Hello, " + name)
}
抽象field
abstract class Person {
val name: String
}
class Student extends Person {
val name: String = "leo"
}
将trait作为接口使用
trait HelloTrait {
def sayHello(name: String)
}
trait MakeFriendsTrait {
def makeFriends(p: Person)
}
class Person(val name: String) extends HelloTrait with MakeFriendsTrait with Cloneable with Serializable {
def sayHello(name: String) = println("Hello, " + name)
def makeFriends(p: Person) = println("Hello, my name is " + name + ", your name is " + p.name)
}
在Trait中定义具体方法
trait Logger {
def log(message: String) = println(message)
}
class Person(val name: String) extends Logger {
def makeFriends(p: Person) {
println("Hi, I'm " + name + ", I'm glad to make friends with you, " + p.name)
log("makeFriends methdo is invoked with parameter Person[name=" + p.name + "]")
}
}
在Trait中定义具体字段
trait Person {
val eyeNum: Int = 2
}
class Student(val name: String) extends Person {
def sayHello = println("Hi, I'm " + name + ", I have " + eyeNum + " eyes.")
}
在Trait中定义抽象字段
trait SayHello {
val msg: String
def sayHello(name: String) = println(msg + ", " + name)
}
class Person(val name: String) extends SayHello {
val msg: String = "hello"
def makeFriends(p: Person) {
sayHello(p.name)
println("I'm " + name + ", I want to make friends with you!")
}
}
为实例混入trait
trait Logged {
def log(msg: String) {}
}
trait MyLogger extends Logged {
override def log(msg: String) { println("log: " + msg) }
}
class Person(val name: String) extends Logged {
def sayHello { println("Hi, I'm " + name); log("sayHello is invoked!") }
}
val p1 = new Person("leo")
p1.sayHello
val p2 = new Person("jack") with MyLogger
p2.sayHello
trait调用链
trait Handler {
def handle(data: String) {}
}
trait DataValidHandler extends Handler {
override def handle(data: String) {
println("check data: " + data)
super.handle(data)
}
}
trait SignatureValidHandler extends Handler {
override def handle(data: String) {
println("check signature: " + data)
super.handle(data)
}
}
class Person(val name: String) extends SignatureValidHandler with DataValidHandler {
def sayHello = { println("Hello, " + name); handle(name) }
}
在trait中覆盖抽象方法
trait Logger {
def log(msg: String)
}
trait MyLogger extends Logger {
abstract override def log(msg: String) { super.log(msg) }
}
混合使用trait的具体方法和抽象方法
trait Valid {
def getName: String
def valid: Boolean = {
getName == "leo"
}
}
class Person(val name: String) extends Valid {
println(valid)
def getName = name
}
trait的构造机制
class Person { println("Person's constructor!") }
trait Logger { println("Logger's constructor!") }
trait MyLogger extends Logger { println("MyLogger's constructor!") }
trait TimeLogger extends Logger { println("TimeLogger's constructor!") }
class Student extends Person with MyLogger with TimeLogger {
println("Student's constructor!")
}
trait field的初始化
trait SayHello {
val msg: String
println(msg.toString)
}
class Person
val p = new {
val msg: String = "init"
} with Person with SayHello
class Person extends {
val msg: String = "init"
} with SayHello {}
trait SayHello {
lazy val msg: String = null
println(msg.toString)
}
class Person extends SayHello {
override lazy val msg: String = "init"
}
trait继承class
class MyUtil {
def printMessage(msg: String) = println(msg)
}
trait Logger extends MyUtil {
def log(msg: String) = printMessage("log: " + msg)
}
class Person(val name: String) extends Logger {
def sayHello {
log("Hi, I'm " + name)
printMessage("Hi, I'm " + name)
}
}