简单的类
image.png
image.png

给类增加字段和方法

image.png

image.png

image.png

创建对象

image.png

编译和执行

  1. class Counter{
  2. private var value = 0
  3. def increment(): Unit ={ value += 1}
  4. def current(): Int ={value}
  5. }
  6. val mycounter = new Counter
  7. mycounter.increment()
  8. println(mycounter.current)

在命令行输入

  1. scala test02.scala

就可以实现功能
也可以使用scala解释器来执行相应的程序 (:load) 在解释器中:代表命令指令
image.png
问题:为什么不用scalac编译
这个例程无法通过
image.png
修改成为单例对象:
image.png
image.png
设置成带参数传入

  1. class Counter{
  2. private var value = 0
  3. def increment(step : Int): Unit ={ value += step}
  4. def current(): Int ={value}
  5. }
  6. object mycounter
  7. {
  8. def main(args:Array[String]){
  9. val mycounter = new Counter
  10. mycounter.increment(5)
  11. println(mycounter.current())
  12. }
  13. }

getter和setter方法

  1. class Counter{
  2. var value = 0
  3. def increment(step : Int): Unit ={ value += step}
  4. def current(): Int ={value}
  5. }
  6. object mycounter
  7. {
  8. def main(args:Array[String]){
  9. val mycounter = new Counter
  10. println(mycounter.value)
  11. mycounter.value=3
  12. mycounter.increment(5)
  13. println(mycounter.current())
  14. }
  15. }

image.png
注意:
image.png
不要乱设置公有属性!
修改 使用value和value_方法

  1. class Counter{
  2. private var privatevalue = 0
  3. def value = privatevalue
  4. def value_=(newValue : int){
  5. if(newValue > 0) privateValue = newValue
  6. }
  7. def increment(step : Int): Unit ={ value += step}
  8. def current(): Int ={value}
  9. }
  10. object mycounter
  11. {
  12. def main(args:Array[String]){
  13. val mycounter = new Counter
  14. println(mycounter.value)
  15. mycounter.value=3 //调用value_这个方法
  16. mycounter.increment(5)
  17. println(mycounter.current())
  18. }
  19. }

构造器

image.png

  1. class Counter{
  2. private var value = 0
  3. private var name = ""
  4. private var mode = 1
  5. def this(name : String){ //第一个辅助构造器
  6. this() //调用主构造器
  7. this.name =name
  8. }
  9. def this(name : String, mode : Int){ //第二个辅助构造器
  10. this(name) //调用前一个辅助构造器
  11. this.mode = mode
  12. }
  13. def increment(step : Int): Unit ={ value += step}
  14. def current(): Int ={value}
  15. def info() : Unit = {printf("name: %s and mode is %d\n",name,mode)}
  16. }
  17. object mycounter
  18. {
  19. def main(args:Array[String]){
  20. val mycounter1 = new Counter //调用主构造器,参数默认为空
  21. val mycounter2 = new Counter("Runner") //第一个辅助
  22. val mycounter3 = new Counter("Timer",2) //第二个辅助,中间会调用第一个辅助构造器
  23. mycounter1.info
  24. mycounter1.increment(1)
  25. printf("Current value is %d\n",mycounter1.current())
  26. mycounter2.info
  27. mycounter2.increment(2)
  28. printf("Current value is %d\n",mycounter2.current())
  29. mycounter3.info
  30. mycounter3.increment(1)
  31. printf("Current value is %d\n",mycounter3.current())
  32. }
  33. }

每个类都是主构造器的

主构造器

image.png
scala中的主构造器改写

  1. class Counter(val name: String , val mode: Int){ //可以直接将参数写在主构造器中
  2. private var privatevalue = 0
  3. def value = privatevalue
  4. def value_=(newValue : int){
  5. if(newValue > 0) privateValue = newValue
  6. }
  7. def increment(step : Int): Unit ={ value += step}
  8. def current(): Int ={value}
  9. def info(): Uint = {printf("name is %s and mode is %d",name,mode)}
  10. //可以直接使用name和mode而不用在类里面进行定义,会自动生成这两个字段
  11. }
  12. object mycounter
  13. {
  14. def main(args:Array[String]){
  15. val mycounter = new Counter
  16. println(mycounter.value)
  17. mycounter.value=3 //调用value_这个方法
  18. mycounter.increment(5)
  19. println(mycounter.current())
  20. }
  21. }