暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

和我一起学习scala (七):面向对象编程之类

程序员雨衣 2019-09-20
172


定义一个简单的类

  1. // 定义类,包含field以及方法

  2. class HelloWorld {

  3. private var name = "leo"

  4. def sayHello() { print("Hello, " + name) }

  5. def getName = name

  6. }


  7. // 创建类的对象,并调用其方法

  8. val helloWorld = new HelloWorld

  9. helloWorld.sayHello()

  10. print(helloWorld.getName) // 也可以不加括号,如果定义方法时不带括号,则调用方法时也不能带括号

getter与setter

  1. // 定义不带private的var field,此时scala生成的面向JVM的类时,会定义为private的name字段,并提供public的getter和setter方法

  2. // 而如果使用private修饰field,则生成的getter和setter也是private的

  3. // 如果定义val field,则只会生成getter方法

  4. // 如果不希望生成setter和getter方法,则将field声明为private[this]

  5. class Student {

  6. var name = "leo"

  7. }


  8. // 调用getter和setter方法,分别叫做name和name_ =

  9. val leo = new Student

  10. print(leo.name)

  11. leo.name = "leo1"

自定义getter与setter

  1. // 如果只是希望拥有简单的getter和setter方法,那么就按照scala提供的语法规则,根据需求为field选择合适的修饰符就好:var、val、private、private[this]

  2. // 但是如果希望能够自己对getter与setter进行控制,则可以自定义getter与setter方法

  3. // 自定义setter方法的时候一定要注意scala的语法限制,签名、=、参数间不能有空格

  4. class Student {

  5. private var myName = "leo"

  6. def name = "your name is " + myName

  7. def name_=(newValue: String) {

  8. print("you cannot edit your name!!!")

  9. }

  10. }


  11. val leo = new Student

  12. print(leo.name)

  13. leo.name = "leo1"

仅暴露field的getter方法

  1. // 如果你不希望field有setter方法,则可以定义为val,但是此时就再也不能更改field的值了

  2. // 但是如果希望能够仅仅暴露出一个getter方法,并且还能通过某些方法更改field的值,那么需要综合使用private以及自定义getter方法

  3. // 此时,由于field是private的,所以setter和getter都是private,对外界没有暴露;自己可以实现修改field值的方法;自己可以覆盖getter方法

  4. class Student {

  5. private var myName = "leo"


  6. def updateName(newName: String) {

  7. if(newName == "leo1") myName = newName

  8. else print("not accept this new name!!!")

  9. }


  10. def name = "your name is " + myName

  11. }

private[this]的使用

  1. // 如果将field使用private来修饰,那么代表这个field是类私有的,在类的方法中,可以直接访问类的其他对象的private field

  2. // 这种情况下,如果不希望field被其他对象访问到,那么可以使用private[this],意味着对象私有的field,只有本对象内可以访问到

  3. class Student {

  4. private var myAge = 0

  5. def age_=(newValue: Int) {

  6. if (newValue > 0) myAge = newValue

  7. else print("illegal age!")

  8. }

  9. def age = myAge

  10. def older(s: Student) = {

  11. myAge > s.myAge

  12. }

  13. }

Java风格的getter和setter方法

  1. // Scala的getter和setter方法的命名与java是不同的,是field和field_=的方式

  2. // 如果要让scala自动生成java风格的getter和setter方法,只要给field添加@BeanProperty注解即可

  3. // 此时会生成4个方法,name: String、name_=(newValue: String): Unit、getName(): String、setName(newValue: String): Unit

  4. import scala.reflect.BeanProperty

  5. class Student {

  6. @BeanProperty var name: String = _

  7. }

  8. class Student(@BeanProperty var name: String)


  9. val s = new Student

  10. s.setName("leo")

  11. s.getName()

辅助constructor

  1. // Scala中,可以给类定义多个辅助constructor,类似于java中的构造函数重载

  2. // 辅助constructor之间可以互相调用,而且必须第一行调用主constructor

  3. class Student {

  4. private var name = ""

  5. private var age = 0

  6. def this(name: String) {

  7. this()

  8. this.name = name

  9. }

  10. def this(name: String, age: Int) {

  11. this(name)

  12. this.age = age

  13. }

  14. }

主constructor

  1. // Scala中,主constructor是与类名放在一起的,与java不同

  2. // 而且类中,没有定义在任何方法或者是代码块之中的代码,就是主constructor的代码,这点感觉没有java那么清晰

  3. class Student(val name: String, val age: Int) {

  4. println("your name is " + name + ", your age is " + age)

  5. }


  6. // 主constructor中还可以通过使用默认参数,来给参数默认的值

  7. class Student(val name: String = "leo", val age: Int = 30) {

  8. println("your name is " + name + ", your age is " + age)

  9. }


  10. // 如果主constrcutor传入的参数什么修饰都没有,比如name: String,那么如果类内部的方法使用到了,则会声明为private[this] name;否则没有该field,就只能被constructor代码使用而已

内部类

  1. // Scala中,同样可以在类中定义内部类;但是与java不同的是,每个外部类的对象的内部类,都是不同的类

  2. import scala.collection.mutable.ArrayBuffer

  3. class Class {

  4. class Student(val name: String) {}

  5. val students = new ArrayBuffer[Student]

  6. def getStudent(name: String) = {

  7. new Student(name)

  8. }

  9. }


  10. val c1 = new Class

  11. val s1 = c1.getStudent("leo")

  12. c1.students += s1


  13. val c2 = new Class

  14. val s2 = c2.getStudent("leo")

  15. c1.students += s2

文章转载自程序员雨衣,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论