scala学习笔记-面向对象编程(11)

定义一个简单的类

 1 // 定义类,包含field以及方法
 2 class HelloWorld {
 3   private var name = "leo"
 4   def sayHello() { print("Hello, " + name) }  
 5   def getName = name
 6 }
 7 
 8 // 创建类的对象,并调用其方法
 9 val helloWorld = new HelloWorld
10 helloWorld.sayHello() 
11 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 
 9 // 调用getter和setter方法,分别叫做name和name_ =
10 val leo = new Student
11 print(leo.name)
12 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 
12 val leo = new Student
13 print(leo.name)
14 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 
 7   def updateName(newName: String) { 
 8     if(newName == "leo1") myName = newName 
 9     else print("not accept this new name!!!")
10   }
11 
12   def name = "your name is " + myName
13 }

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 
10 val s = new Student
11 s.setName("leo")
12 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 
 7 // 主constructor中还可以通过使用默认参数,来给参数默认的值
 8 class Student(val name: String = "leo", val age: Int = 30) {
 9   println("your name is " + name + ", your age is " + age)
10 }
11 
12 // 如果主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 
11 val c1 = new Class
12 val s1 = c1.getStudent("leo")
13 c1.students += s1
14 
15 val c2 = new Class
16 val s2 = c2.getStudent("leo")
17 c1.students += s2

 

转载于:https://www.cnblogs.com/HeQiangJava/p/6711508.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值