• scala类


    1.简单类和无参方法

    class Demo {
      private var value = 0
    
      def increment(): Unit = {
        value += 1
      }
    
      def current = value
    
    }
    
    
    object Test {
    
      def main(args: Array[String]): Unit = {
        //创建对象
        val demo = new Demo() //或者是Demo()
    
        demo.increment() //取值不加()  改值添加()
        val result = demo.current //定义函数没有加() 时,不能加()
        println(result)
        
      }
    }

    2.带getter/setter属性

    class Demo {
      var value = 0    //public setter getter方法也是public   private 修饰时,则getter setter也是 private修饰
    
    }
    
    
    object Test {
    
      def main(args: Array[String]): Unit = {
        //创建对象
        val demo = new Demo()
    
        //属性的getter setter方法  方法名分别是value value_  java中setXxx getXxx
        demo.value = 10
        println(demo.value)
    
      }
    }
    class Counter {
      private var privateValue = 0
    
      //私有变量,外界无法直接访问
      def value = privateValue
    
      //定义一个方法,方法名为我们想要的字段的名称,代替getter
      def value_=(newValue: Int) {
        //主语 value_= 是方法名字
        privateValue = newValue
      } //注意,scala中默认方法是public的
    }
    
    object MyCounter {
      def main(args: Array[String]): Unit = {
        val myCounter = new Counter
    
        println(myCounter.value); //调用value方法访问value,相当于getter
        myCounter.value = 3; //为value设置新值,相当于setter
        println(myCounter.value); //调用value方法访问value,相当于getter
    
      }
    }
        //字段私有,则getter setter方法也是私有的
        //字段是val,则只有getter没有setter
        //不需要getter setter 方法,则定义为private[this]

    3.只带getter属性

    class Counter {
      val privateValue = 0   // 会生成带final修饰的私有字段和getter方法,没有setter方法
    
    }
    class Counter {
      //不能直接修改,但是通过其他方法修改
      private var privateValue = 0
    
      def increment() = {
        privateValue += 1
      }
    
      def current = privateValue
    
    }
    
    object MyCounter {
      def main(args: Array[String]): Unit = {
        val counter = new Counter()
        counter.increment()
        println(counter.current)
    
      }
    }

    4.对象私有字段

    class Counter {
    
      private var privateValue = 0
    
      def increment() = {
        privateValue += 1
      }
    
      def isLess(other: Counter) = {
        privateValue < other.privateValue //other也是Counter类型的,也有一个privateValue属性
      }
    
    }
    class Counter {
    
      private[this] var privateValue = 0   //private[this]修饰对象私有,不会生成setter getter方法
                                          // private修饰,类私有,生成私有的setter getter方法
    
      def increment() = {
        privateValue += 1
      }
    
      def isLess(other: Counter) = {
    //    privateValue < other.privateValue  //报错,private[this] 修饰,只能访问当前对象的privateValue字段
        //而不能访问同样是Counter类型其他对象的该字段
      }
    
    }

    5.bean属性

    import scala.reflect.BeanProperty
    
    //主构造器
    class Counter(@BeanProperty var name1: String) {
    
    
      //想要生成跟java一样的setXxx getXxx scala将字段标注@BeanProperty,这样的方法会自动生成
      @BeanProperty var name: String = _
      //会生成四个方法
      //name:String
      //name_=(newName:String):Unit
      //getName:String
      //setName(newName:String):Unit
      
    }

    6.辅助构造器

    import scala.reflect.BeanProperty
    
    //主构造器
    class Person {
    
    
      //辅助构造器
      //名称:this
      //调用其他已经定义的辅助构造器为开始
    
      private var name: String = _
      private var age: Int = _
    
      def this(name: String) = {
        this() //调用主构造函数
        this.name = name
      }
    
      def this(name: String, age: Int) = {
        this(name)
        this.age = age
      }
    
    
    }
    
    object MyCounter {
      def main(args: Array[String]): Unit = {
        val p1 = new Person() //主构造
        val p2 = new Person("zhangsan")
        val p3 = new Person("zhangsan", 10)
    
      }
    }

    7.主构造器

    //主构造器
    class Person(val name: String, val age: Int) { //参数被编译成字段
    
      println("创建对象会执行")
    
      def description = name + " is " + age + " years old"
    
    }
    
    object MyCounter {
      def main(args: Array[String]): Unit = {
    
        val p1 = new Person("zhangsan", 20)
        println(p1.description)
    
      }
    }
    //主构造器
    class Person private(val name: String) { //主构造器私有
      private var age: Int = _
    
      def this(name: String, age: Int) = {
        this(name)
        this.age = age
      }
    }
    
    object MyCounter {
      def main(args: Array[String]): Unit = {
    
        val p1 = new Person("zhangsan", 20) //只能通过辅助构造器创建对象
      }
    }

    8.嵌套类

    import scala.collection.mutable.ArrayBuffer
    //嵌套类:类中定义类
    
    class Network {
    
      class Member(name: String) {
        val contacts = new ArrayBuffer[Member]()
      }
    
      private val members = new ArrayBuffer[Member]()
    
      def join(name: String) = {
        val m = new Member(name)
        members += m
        m
      }
    }
    
    object MyCounter {
      def main(args: Array[String]): Unit = {
    
        val chatter = new Network
        val myFace = new Network //chatter.Member myFace.Member是不同的类
        val fred = chatter.join("Fred")
        val wilam = chatter.join("Wilam")
        fred.contacts += wilam
    
        val barney = myFace.join("Barney")
        //    fred.contacts += barney  //报错,barney是 myFace.Member类型
    
      }
    }
    //解决方式一:伴生对象中定义
    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 } } object Network { class Member(name: String) { val contacts = new ArrayBuffer[Member]() } def main(args: Array[String]): Unit = { val chatter = new Network val myFace = new Network //chatter.Member myFace.Member是不同的类 val fred = chatter.join("Fred") val wilam = chatter.join("Wilam") fred.contacts += wilam val barney = myFace.join("Barney") fred.contacts += barney } }
    //解决方式二:;类型投影 Network#Member 表示人任何Network的Member

    import
    scala.collection.mutable.ArrayBuffer //嵌套类:类中定义类 class Network { class Member(name: String) { val contacts = new ArrayBuffer[Network#Member]() } private val members = new ArrayBuffer[Network#Member]() def join(name: String) = { val m = new Member(name) members += m m } } object MyCounter { def main(args: Array[String]): Unit = { val chatter = new Network val myFace = new Network //chatter.Member myFace.Member是不同的类 val fred = chatter.join("Fred") val wilam = chatter.join("Wilam") fred.contacts += wilam val barney = myFace.join("Barney") fred.contacts += barney } }
  • 相关阅读:
    斐波那契数列递归算法和非递归算法以及其时间复杂度分析
    Python为什么不需要swap(a, b)
    学习MySQL中遇到的问题(net start mysql; 服务名无效。 请键入 NET HELPMSG 2185 以获得更多的帮助)
    python 3以上版本使用pickle.load读取文件报UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6
    Mysql 服务无法启动 服务没有报告任何错误
    Keras和tensorflow的区别
    简单易懂的softmax交叉熵损失函数求导
    Ajax 请求
    Ajax 新建对象
    jquery Ajax noConflict()
  • 原文地址:https://www.cnblogs.com/yin-fei/p/10827657.html
Copyright © 2020-2023  润新知