• Scala 类


    Scala 类

    简单类和无参方法

    class HelloWorld {
      private val value = 0
    
      def increment(): Int = value + 1
    
      def current(): Int = value
    
      def printValue: Unit = println(value)
    }
    
    
    val helloWorld = new HelloWorld()
    helloWorld.current()
    helloWorld.increment()
    helloWorld.printValue
    helloWorld.increment
    helloWorld.printValue
    

    注意

    类不声明为public,一个Scala源文件可以包含多个类。所有的这些类都具有公有可见性。调用无参方法时,可以加()也可以不加;如果方法定义中不带括号,那么就不能带括号。

    Getter、Setter方法

    • get方法的名字就是字段的名字,set方法名字为字段名称加下划线age_使用时age=即可
    • 如果字段是私有的,那么getter和setter方法也是私有的
    • 如果字段为val声明的常量,那么只会生成get方法
    • 如果你不需要任何get和set方法可以将字段声明为 private
    class HelloWorld {
      var age = 0
      var name = "upuptop"
    }
    
    
    val helloWorld = new HelloWorld()
    
    println(helloWorld.age)
    println(helloWorld.name)
    helloWorld.name="pyfysf"
    helloWorld.age=20
    println(helloWorld.age)
    println(helloWorld.name)
    

    对象私有字段

    对象的私有字段不生成getter和setter方法

    Bean属性

    Javabeans规范定义了Java的属性是像 getXxx()setXxx()的方法,许多java的工具都依赖这个命名习惯,为了java互操作性,将Scala字段加@BeanProperty时,这样的方法会自动生成

    import scala.beans.BeanProperty
    
    class HelloWorld {
      @BeanProperty
      var age = 0
      @BeanProperty
      var name = "upuptop"
    }
    
    
    val helloWorld = new HelloWorld()
    
    println(helloWorld.getAge)
    println(helloWorld.getName)
    helloWorld.setName("pyfysf")
    helloWorld.setAge(20)
    println(helloWorld.getAge)
    println(helloWorld.getName)
    

    每个字段生成四个方法:

    • name:String
    • name_=(newValue:String):Unit
    • getName():String
    • setName(newValue:String):Unit
      在这里插入图片描述

    构造器

    在这里插入图片描述
    Scala的类构造器分为主构造器和辅助构造器。
    1.主构造器的参数直接放置在类名之后:

    import scala.beans.BeanProperty
    
    class Person(@BeanProperty val name: String, private var age: Int) {
      def desc = s"${name} is ${age} years old"
    
      def birthday(): Int = {
        age + 1
      }
    }
    
    val upuptop = new Person("upuptop", 20)
    upuptop.getName
    upuptop.birthday()
    upuptop.desc
    

    2.主构造器会执行类中定义的所有语句

    import scala.beans.BeanProperty
    
    class Person(@BeanProperty val name: String, private var age: Int) {
      println("init person")
      
      def desc = s"${name} is ${age} years old"
    
      def birthday(): Int = {
        age + 1
      }
    }
    
    //打印结果:init person
    val upuptop = new Person("upuptop", 20)
    

    3.可以通过private设置主构造器的私有属性
    4.如果参数至少要被一个方法使用,该参数自动升格为字段,否则改参数将不被保存为字段
    5.辅助构造器名称为this,通过不同参数进行区分,每个辅助构造器都必须以主构造器或者已经定义的辅助构造器的调用开始。

    
    class Person3 {
      private var name = ""
      private var age = 0
    
      def this(name: String) { // An auxiliary constructor
        this() // Calls primary constructor
        this.name = name
      }
      def this(name: String, age: Int) { // Another auxiliary constructor
        this(name) // Calls previous auxiliary constructor
        this.age = age
      }
    
      def description = name + " is " + age + " years old"
    }
    
    val p11 = new Person3 // Primary constructor
    val p21 = new Person3("Fred") // First auxiliary constructor
    val p31= new Person3("Fred", 42) // Second auxiliary constructor
    
    p11.description
    p21.description
    p31.description
    
    

    辅助构造器必须先调用主构造器,this()

    嵌套类

    在 Scala中,你几乎可以在任何语法结构中嵌套任何语法结构,在函数中定义函数,在类中定义类。Java中的内部类从属于外部类。 Scala中每一个实例都有一个内部类,内部类从属于实例.

    import scala.collection.mutable.ArrayBuffer
    
    class Network {
      private val members = new ArrayBuffer[Network.Member]
    
      def join(name: String) = {
        val m = new Network.Member(name)
        members += m
        m
      }
      def description = "a network with members " + 
        (for (m <- members) yield m.description).mkString(", ")
    }
    
    object Network {
      class Member(val name: String) {
        val contacts = new ArrayBuffer[Member]
        def description = name + " with contacts " + 
          (for (c <- contacts) yield c.name).mkString(" ")
      }
    }
    
    val chatter = new Network
    val myFace = new Network
    
    val fred = chatter.join("Fred")
    val wilma = chatter.join("Wilma")
    fred.contacts += wilma // OK
    val barney = myFace.join("Barney")
    fred.contacts += barney // Also OK
    println("chatter is " + chatter.description)
    println("myFace is " + myFace.description)
    
    
    import scala.collection.mutable.ArrayBuffer
    
    class Network(val name: String) { outer =>  
      class Member(val name: String) {
        val contacts = new ArrayBuffer[Member]
        def description = name + " inside " + outer.name
      }
    
      private val members = new ArrayBuffer[Member]
    
      def join(name: String) = {
        val m = new Member(name)
        members += m
        m
      }
    }
    
    val chatter = new Network("Chatter")
    val myFace = new Network("MyFace")
    
    val fred = chatter.join("Fred")
    println(fred.description);
    val barney = myFace.join("Barney")
    println(barney.description);
    
    

    本博客仅为博主学习总结,感谢各大网络平台的资料。蟹蟹!!

  • 相关阅读:
    【完结】php+MySQL实战案例【九】总结及源码分享
    php+MySQL实战案例【八】用户注册
    php+MySQL实战案例【七】数据编辑、删除
    php+MySQL实战案例【六】数据分页查询
    php+MySQL实战案例【五】新增用户
    php+MySQL实战案例【四】管理员登录
    php+MySQL实战案例【三】数据表设计
    php+MySQL实战案例【二】php数据库辅助类
    5700清空所有配置的方法
    华为设备默认console密码
  • 原文地址:https://www.cnblogs.com/shaofeer/p/11154283.html
Copyright © 2020-2023  润新知