Scala_类

简单类

  • 最简单的类的定义形式是:

class Test1 {
 //这里定义类的字段和方法
}
  • 可以使用new关键字来生成对象

var test = new Test1()

给类增加字段和方法

  • Unit后面的等号和大括号里面,包含了该方法要执行的具体操作语句

class Test1 {
 private var value = 0
 def increment():Unit = {
   value += 1
}
 def current():Int = {
   value
}
}
  • 如果大括号里面只有一行语句,那么也可以直接去掉大括号,写成下面的形式:

class Test1 {
 private var value = 0
 def increment():Unit = value += 1
 def current():Int = {
   value
}
}
  • 或者,还可以去掉返回值类型和等号,只保留大括号,如下:

class Test1 {
 private var value = 0
 def increment() {value += 1}
 def current():Int = {
   value
}
}

创建对象

  • 我们新建对象,并调用其中的方法:

class Test1 {
 private var value = 0
 def increment() {value += 1}
 def current():Int = {
   value
}
}
object Test1{
 def main(args: Array[String]): Unit = {
   var test = new Test1()
   test.increment()
   println(test.current)
}
}
  • 从上面代码可以看出,Scala在调用无参方法时,是可以省略方法名后 面的圆括号的

编译和执行

  • 新建一个test1.scala代码文件

class Test1 {
  private var value = 0
  def increment() {value += 1}
  def current():Int = {
    value
  }
}
var test = new Test1()
test.increment()
println(test.current)
  • 在Linux Shell命令提示符下,使用scala命令执行这个代码文件:

[root@slave2 ~]# scala test1.scala
1
  • 也可以进入到Scala解释器下面去执行TestCounter.scala 首先启动Scala解释器,如下:

[root@slave2 ~]# scala
Welcome to Scala version 2.11.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_171).
Type in expressions to have them evaluated.
Type :help for more information.
  • 进入scala命令提示符状态以后,可以在里面输入如下命令:

scala> :load test1.scala
Loading test1.scala...
defined class Test1
test: Test1 = Test1@1554909b
1
  • 退出Scala解释器的命令如下:

scala> :quit
[root@slave2 ~]#
  • 下面尝试一下,看看是否可以使用scalac命令对这个 test1.scala文件进行编译,如下:

[root@slave2 ~]# scalac test1.scala 
test1.scala:8: error: expected class or object definition
var test = new Test1()
^
test1.scala:9: error: expected class or object definition
test.increment()
^
test1.scala:10: error: expected class or object definition
println(test.current)
^
three errors found
  • 执行上述scalac命令后,会出现一堆错误,无法编译。 为什么呢?

  • 原因:声明都没有被封装在对象中,因此,无法编译成 JVM字节码

  • 在Test2.scala中输入以下代码:

package day01
class Test1 {
 private var value = 0
 def increment() {value += 1}
 def current():Int = {
   value
}
}
object Test1{
 def main(args: Array[String]): Unit = {
   var test = new Test1()
   test.increment()
   println(test.current)
}
}
  • 使用scalac命令编译这个代码文件,并用scala命令执行,如下:

[root@slave2 ~]# ll
total 32716
-rw-------. 1 root root     1260 Jul  2 19:02 anaconda-ks.cfg
-rw-r--r--. 1 root root 33485703 May 15 00:56 elasticsearch-5.5.2.tar.gz
-rw-r--r--. 1 root root      171 Aug 18 05:56 test1.scala
-rw-r--r--. 1 root root      245 Aug 18 06:15 test2.scala
[root@slave2 ~]# scalac test2.scala
[root@slave2 ~]# ll
total 32724
-rw-------. 1 root root     1260 Jul  2 19:02 anaconda-ks.cfg
-rw-r--r--. 1 root root 33485703 May 15 00:56 elasticsearch-5.5.2.tar.gz
-rw-r--r--. 1 root root      171 Aug 18 05:56 test1.scala
-rw-r--r--. 1 root root     1241 Aug 18 06:15 Test2.class
-rw-r--r--. 1 root root      763 Aug 18 06:15 Test2$.class
-rw-r--r--. 1 root root      245 Aug 18 06:15 test2.scala
[root@slave2 ~]# scala Test2
1

getter和setter方法

  • 给类中的字段设置值以及读取值,在Java中是通过getter和setter方法实现的

  • 在Scala中,也提供了getter和setter方法的实现,但是并没有定义成getXxx和 setXxx

  • value变成私有字段以后,Scala又没有提供getter和setter方法,怎么可以访问 value字段呢?解决方案是,在Scala中,可以通过定义类似getter和setter的方 法,分别叫做value和value_=,具体如下:

class Test2 {
 private var privateValue = 0 //私有字段
 def value = privateValue //定义一个方法,类似Java中的setter方法
 def value_=(newValue:Int): Unit ={
   if(newValue > 0) privateValue = newValue
}
 def increment() {privateValue += 1}
 def current():Int = {
   privateValue
}
}
object Test{
 def main(args: Array[String]): Unit = {
   var test = new Test2()
   println(test.value)
   test.value = 3
   println(test.value)
   test.increment()
   println(test.current())
}
}

辅助构造器

  • Scala构造器包含1个主构造器和若干个(0个或多个)辅助构造器

  • 辅助构造器的名称为this,每个辅助构造器都必须调用一个此前已经定义的 辅助构造器或主构造器

  • 下面定义一个带有辅助构造器的类,我们对上面的Test2类定义进行修改:

class Test2 {
 private var privateValue = 0 //私有字段
 private var name = ""
 private var mode = 1

 def this(name:String){ //第一个辅助构造器
   this() //调用主构造器
   this.name = name
}
 def this(name:String,mode:Int){
   this(name)
   this.mode = mode
}
 def increment(step : Int) {privateValue += step}
 def current():Int = {privateValue}
 def info()={printf("Nmae:%s and mode is %d\n",name,mode)}
}
object Test{
 def main(args: Array[String]): Unit = {
   var test1 = new Test2 //主构造器
   var test2 = new Test2("Runner") //第一个辅助构造器
   var test3 = new Test2("Timer",2)//第二个辅助构造器
   test1.info()
   test1.increment(1)
   printf("current Value is : %d\n",test1.current()) //显示计数器当前值
   test2.info()
   test2.increment(2)
   printf("current Value is : %d\n",test2.current()) //显示计数器当前值
   test3.info()
   test3.increment(3)
   printf("current Value is : %d\n",test3.current()) //显示计数器当前值
}
}

主构造器

  • Scala的每个类都有主构造器。但是,Scala的主构造器和Java有着明显的不 同,Scala的主构造器是整个类体,需要在类名称后面罗列出构造器所需的所有参数,这些参数被编译成字段,字段的值就是创建对象时传入的参数的值

  • 对于上面给计数器设置name和mode的例子,刚才我们是使用辅助构造器来 对name和mode的值进行设置,现在我们重新来一次,这次我们转而采用主构 造器来设置name和mode的值。

class Test3(val name:String,val mode:Int){
 private var privateValue = 0 //私有字段
 def increment(step : Int) {privateValue += step}
 def current():Int = {privateValue}
 def info()={printf("Nmae:%s and mode is %d\n",name,mode)}
}
object Test33{
 def main(args: Array[String]): Unit = {
   var test3 = new Test3("Timer",2)//第二个辅助构造器
   test3.info()
   test3.increment(1)
   printf("current Value is : %d\n",test3.current()) //显示计数器当前值
}
}

猜你喜欢

转载自www.cnblogs.com/zxbdboke/p/10466295.html