• 5、scala面向对象-类


    一、类

    1、定义类

    ##定义并调用
    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
    class HelloWord {
      private var name = "Leo"
      def sayHello() {print("Hello, " + name)}
      def getName = name
    }
    
    // Exiting paste mode, now interpreting.
    
    defined class HelloWord
    
    scala> val helloWord = new HelloWord
    helloWord: HelloWord = HelloWord@eba64a9
    
    scala> helloWord.sayHello()
    Hello, Leo
    scala> 
    
    scala> print(helloWord.getName)
    Leo


    2、getter和setter

    我们在用Java的时候经常把一些字段定义为private类型来完成封装,这样外界就无法访问。
    如果外界访问或者修改该字段的时候,只能通过该字段提供的getter和setter方法来实现。
    在Scala中是没有getter和setter一说的。
    用了value 和 value_= 来分别代替了getter和setter。
    
    
    ■ 如果字段是私有的,则getter和setter方法也是私有的   
    
      就是说明,当我们定义一个字段为私有的时候,自动生成的getter和setter也不能被外界使用了。
    
           就是我们不能在外界使用  点+字段  的方式来访问或者修改该字段了。
    
      我们可以通过自己改写scala的getter和setter来完成对私有变量的访问和修改,如上述。
    
    ■ 如果字段是val,则只有getter方法被生成 
    
      当我们需要getter和setter的时候,可以定义变量为var
    
      当我们只需要getter不需要setter的时候,我们可以定义变量为val
    
    ■ 如果你不需要任何getter或setter,可以将字段声明为private[this]
    
    我们要么在对私有变量设置访问方法的时候
    要么加上getter方法(对val字段而言)要么加上getter和setter方法(对var字段而言)
    但在Scala中,你不能实现只写属性,即带有setter但不带getter的属性。
    
    
    
    ##
    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
    class Student {
      var name = "leo"
    }
    
    // Exiting paste mode, now interpreting.
    
    
    scala> val s = new Student
    s: Student = Student@4cdbe50f
    
    scala> s.name
    res2: String = leo
    
    scala> s.name = "jack"
    s.name: String = jack
    
    scala> s.name
    res3: String = jack


    3、自定义getter  setter

    //如果只是希望拥有简单的getter和setter方法,那么就按照scala提供的语法规则,根据需求为field选择合适的修饰符就好:var、val、private、private[this]
    //但是如果希望能够自己对getter与setter进行控制,则可以自定义getter与setter方法
    //自定义setter方法的时候一定要注意scala的语法限制,签名、=、参数间不能有空格
    
    
    ##
    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
    class Student {
      private var myName = "leo"
      def name = "your name is " + myName
      def name_=(newName: String) {
        print("you cannot edit your name!")
      }
    }
    
    // Exiting paste mode, now interpreting.
    
    defined class Student
    
    scala> val s = new Student
    s: Student = Student@52f759d7
    
    scala> s.name
    res4: String = your name is leo
    
    scala> s.name = "leo1"
    you cannot edit your name!s.name: String = your name is leo


    4、仅暴露field的getter方法

    //如果你不希望field有setter方法,则可以定义为val,但是此时就再也不能更改field的值了
    //但是如果希望能够仅仅暴露出一个getter方法,并且还能通过某些方法更改field的值,那么需要综合使用private以及自定义getter方法
    //此时,由于field是private的,所以setter和getter都是private,对外界没有暴露;自己可以实现修改feld值的方法;自己可以覆盖getter方法
    
    
    
    ##
    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
    class Student {
      private var myName = "leo"
      
      def updateName(newName: String) {
        if (newName == "leo1") myName = newName
        else println("not accept this new name, " + newName)
      }
      
      def name = myName
    }
    
    // Exiting paste mode, now interpreting.
    
    defined class Student
    
    scala> val s = new Student
    s: Student = Student@4aa8f0b4
    
    scala> s.updateName("leo1")
    
    scala> s.updateName("leo2")
    not accept this new name, leo2
    
    scala> s.name
    res2: String = leo1


    5、private[this]的使用

    //如果将field使用private来修饰,那么代表这个field是类私有的,在类的方法中,可以直接访问类的其他对象的private field
    //这种情况下,如果不希望field被其他对象访问到,那么可以使用private[this],意味着对象私有的field,只有本对象内可以访问到
    
    
    
    ##
    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
    class Student {
      private[this] var myAge = 0
      def age_=(newAge: Int) {
      if (newAge > 0) myAge = newAge
      else println("illegal age!!")
      }
      def age = myAge
      def older(s: Student) = {
      myAge > s.myAge
      }
    }
    
    // Exiting paste mode, now interpreting.
    
    <console>:22: error: value myAge is not a member of Student
             myAge > s.myAge
                       ^


    6、Java风格的getter和setter方法

    //Scala的getter和setter方法的命名与java是不同的,是field和field_=的方式
    //如果要让scala自动生成java风格的getter和seter方法,只要给field添加@BeanProperty注解即可
    //此时会生成4个方法,name:String、name_=(newValue:String):Unit、getName():String、setName(newValue:String):Unit
    
    
    
    ###

    scala> import scala.reflect.BeanProperty
    <console>:10: error: object BeanProperty is not a member of package reflect
            import scala.reflect.BeanProperty
                   ^

    scala> import scala.beans.BeanProperty
    import scala.beans.BeanProperty

    scala> :paste
    // Entering paste mode (ctrl-D to finish)

    class Student {
       @BeanProperty var name: String = _
    }

    // Exiting paste mode, now interpreting.

    defined class Student

    scala> val s = new Student
    s: Student = Student@59690aa4

    scala> s.setName("leo")

    scala> s.getName()
    res1: String = leo

    scala> s.setName("jack")

    scala> s.getName()
    res3: String = jack


    ###在主构造函数方式加注解

    scala> class Student(@BeanProperty var name: String)
    defined class Student

    scala> val s = new Student("leo")
    s: Student = Student@5f9d02cb

    scala> s.getName()
    res4: String = leo

    scala> s.setName("jen")

    scala> s.getName()
    res6: String = jen


    7、辅助constructor

    Scala中,可以给类定义多个辅助constructor,类似于Java中的构造函数重载,辅助constructor之间可以互相调用,而且必须第一行调用主constructor。
    
    
    ###
    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
    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
      }
    }
    
    // Exiting paste mode, now interpreting.
    
    defined class Student
    
    scala> val s1 = new Student
    s1: Student = Student@78b729e6
    
    scala> val s1 = new Student("leo")
    s1: Student = Student@1fe20588
    
    scala> val s1 = new Student("leo",30)
    s1: Student = Student@647e447


    8、主constructor

    //Scala中, 主constructor是与类名放在一起的,与java不同
    //而且类中,没有定义在任何方法或者是代码块之中的代码,就是主constructor的代码,这点感觉没有java那么清晰
    
    ###
    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
    class Student(val name: String, val age: Int) {
      println("your name is " + name + ", your age is " + age)
    }
    
    // Exiting paste mode, now interpreting.
    
    defined class Student
    
    scala> val s = new Student("leo", 30)
    your name is leo, your age is 30
    s: Student = Student@6069db50
    
    
    
    主construntor中还可以通过使用默认参数,来给参数默认的值。
    
    ###
    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
    class Student(val name: String = "leo", val age: Int = 30) {
      println("your name is " + name + ", your age is " + age)
    }
    
    // Exiting paste mode, now interpreting.
    
    defined class Student
    
    scala> val s = new Student
    your name is leo, your age is 30
    s: Student = Student@682b2fa
    
    
    
    如果主constructor传入的参数什么修饰都没有,比如name:String,那么如果类内部的方法使用到了,则会声明为private[this] name,
    否则没有该field,就只能被constructor代码使用而已。
    
    ###
    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
    class Student(name: String, age: Int) {
      println("your name is " + name + ", your age is " + age)
    }
    
    // Exiting paste mode, now interpreting.
    
    defined class Student
    warning: previously defined object Student is not a companion to class Student.
    Companions must be defined together; you may wish to use :paste mode for this.
    
    scala> val s = new Student("leo", 30)
    your name is leo, your age is 30
    s: Student = Student@58a9760d


    9、内部类

    Scala中,同样可以在类中定义内部类,但是与Java不同的是,每个外部类的对象的内部类,都是不同的类。
    
    c2.Student类,c1.Student类,是不同的外部类的实例的不同的类。
    
    
    ###
    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
    import scala.collection.mutable.ArrayBuffer
    
    class Class {
      class Student(val name: String) 
      val students = new ArrayBuffer[Student]()
      def getStudent(name: String) = {
        new Student(name) 
      } 
    }
    
    // Exiting paste mode, now interpreting.
    
    import scala.collection.mutable.ArrayBuffer
    defined class Class
    
    scala> val c1 = new Class
    c1: Class = Class@50b5ac82
    
    scala> val s1 = c1.getStudent("leo")
    s1: c1.Student = Class$Student@45099dd3
    
    scala> c1.students += s1
    res0: c1.students.type = ArrayBuffer(Class$Student@45099dd3)
    
    scala> val c2 = new Class
    c2: Class = Class@2783717b
    
    scala> val s2 = c2.getStudent("leo")
    s2: c2.Student = Class$Student@7e985ce9
    
    scala> c1.students += s2
    <console>:16: error: type mismatch;
     found   : c2.Student
     required: c1.Student
           c1.students += s2
                          ^
  • 相关阅读:
    SQL Server 各版本安装包分享
    你真的了解数据库工程师吗?写给想从事数据库方面工作的朋友
    SQL Server中CASE WHEN的用法
    SQL Server存储过程用法介绍
    python---re模块
    python----openpyxl模块
    selenium(一)---selenium基础
    数据库---MySQL(三)
    数据库---MySql(二)-pymysql
    数据库---MySQL(一)
  • 原文地址:https://www.cnblogs.com/weiyiming007/p/11008192.html
Copyright © 2020-2023  润新知