1、 定义一个简单的类
// 定义类,包含field以及方法
class HelloWorld {
private var name="wei"
def sayHello() { print("Hello," + name)}
def getName = name
}
// 创建类对象,并调用其方法
val helloWorld = new HelloWorld
helloWorld.sayHello()
helloWorld.getName //如果方法定义不加括号,则调用时也不能加括号
2、field的getter与setter详解
getter,setter
- 定义不带private的var field,此时scala生成面向对象的JVM类时,会自动定义为private字段,并提供public的getter,setter方法
- 如果使用private修饰field,则生成的getter和setter也是private的
- 如果定义val field,则只会生成getter方法
- 如果不希望生成getter,setter方法,则将field声明为private[this]
class Student {
var name = "wei"
}
//调用getter,setter方法
var s = new Student
s.name //getter
s.name="lea" //setter
自定义getter,setter方法
自定义setter方法时,_= 左右不能有空格
class Student {
private var myName = "wei"
//getter方法定义
def name = "your name is" + myName
//setter方法定义
def name_=(newValue: String){
print("you cannot edit your name!!")
}
}
// 调用自定义方法
val wei=new Student
wei.name
wei.name="sdf"
private[this]的使用
如果将field使用private修饰,那么代表这个field是类私有的,在类的方法中可以直接访问类的其他对象的private field。
如果不希望field被同个类的其他对象访问到,那么可以使用private[this],意味着对象私有的field,只有本对象内可以访问到。
下面是private的情况:
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
}
}
scala> var s1 = new Student
s1: Student = Student@74af4b0
scala> s1.age=20
s1.age: Int = 20
scala> var s2 = new Student
s2: Student = Student@41d0b71d
scala> s2.age=25
s2.age: Int = 25
scala> s1.older(s2)
res7: Boolean = false
下面是private[this]的情况:
class Student {
private[this] 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
}
}
// 因为访问不了其他对象的myAge,索引类的定义报如下错误
<pastie>:21: error: value myAge is not a member of Student
myAge > s.myAge
Java风格的getter和setter方法
scala的getter和setter方法的命名与java不同,是field= , field_=方式
如果要让scala自动生成java风格的getter,setter方法,只要给field添加@BeanProperty注解即可
此时会生成4个方法:name:String, name_=(newValue: String):Unit,getName():String,setName(newValue: String):Unit
// 方法1
import scala.beans.BeanProperty
class Student{
@BeanProperty var name:String=_
}
scala> var s = new Student
s: Student = Student@262ac4cc
scala> s.setName("wei")
scala> s.getName()
res9: String = wei
// 方法2
import scala.beans.BeanProperty
class Student(@BeanProperty var name:String)
scala> var s = new Student("wei")
s: Student = Student@7bcf808e
scala> s.setName("wei1")
scala> s.getName()
res11: String = wei1
3、constructor详解
辅助constructor
scala中可以给类定义多个辅助constructor,类似于java中的构造函数重载,辅助constructor之间可以相互调用,而且必须第一行调用主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
}
}
scala> var s = new Student("wei")
s: Student = Student@2d279cd2
scala> var s1 = new Student("wei", 30)
s1: Student = Student@52eaa71c
主constructor
scala中主constructor与类名放在一起,而且类中不会有主constructor的代码
class Student(val name:String,val age:Int){
println("your name is" + name +", your age is" + age)
}
//主constructor还可以使用默认参数,来给参数默认的值
class Student(val name:String="wei",val age:Int=20){
println("your name is" + name +", your age is" + age)
}
如果主constructor传入的参数什么修饰都没有,比如 name:String,那么如果类内部的方法使用到了该参数,则会声明为private[this] name;如果没有被方法用到,则只能被constructor代码使用而已
4、内部类介绍
scala可以在类中定义内部类,但是要注意,每个外部类对象的内部类都是不同的类
import scala.collection.mutable.ArrayBuffer
class Class{
class Student(val name:String){}
val students=new ArrayBuffer[Student]
def getStudent(name:String)={
new Student(name)
}
}
scala> val c1 = new Class
c1: Class = Class@253fefea
scala> val s1=c1.getStudent("wei")
s1: c1.Student = Class$Student@55b87372
scala> c1.students += s1
res13: c1.students.type = ArrayBuffer(Class$Student@55b87372)
scala> val c2 = new Class
c2: Class = Class@71dbf2b3
scala> val s2=c2.getStudent("wei2")
s2: c2.Student = Class$Student@69728f4d
scala> c1.students += s2
<console>:20: error: type mismatch;
found : c2.Student
required: c1.Student
c1.students += s2
^