Scala 类
Scala 類
簡單類和無參方法
class HelloWorld {private val value = 0def increment(): Int = value + 1def current(): Int = valuedef 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
對象私有字段
對象的私有字段不生成getter和setter方法
Bean屬性
Javabeans規(guī)范定義了Java的屬性是像 getXxx()和 setXxx()的方法,許多java的工具都依賴這個命名習慣,為了java互操作性,將Scala字段加@BeanProperty時,這樣的方法會自動生成
import scala.beans.BeanPropertyclass HelloWorld {@BeanPropertyvar age = 0@BeanPropertyvar 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.主構造器的參數(shù)直接放置在類名之后:
2.主構造器會執(zhí)行類中定義的所有語句
import scala.beans.BeanPropertyclass 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.如果參數(shù)至少要被一個方法使用,該參數(shù)自動升格為字段,否則改參數(shù)將不被保存為字段
5.輔助構造器名稱為this,通過不同參數(shù)進行區(qū)分,每個輔助構造器都必須以主構造器或者已經(jīng)定義的輔助構造器的調用開始。
輔助構造器必須先調用主構造器,this()
嵌套類
在 Scala中,你幾乎可以在任何語法結構中嵌套任何語法結構,在函數(shù)中定義函數(shù),在類中定義類。Java中的內部類從屬于外部類。 Scala中每一個實例都有一個內部類,內部類從屬于實例.
import scala.collection.mutable.ArrayBufferclass Network {private val members = new ArrayBuffer[Network.Member]def join(name: String) = {val m = new Network.Member(name)members += mm}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 Networkval 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.ArrayBufferclass 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 += mm} }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);本博客僅為博主學習總結,感謝各大網(wǎng)絡平臺的資料。蟹蟹!!
轉載于:https://www.cnblogs.com/upuptop/p/11154283.html
總結
- 上一篇: PJzhang:互联网是有国界
- 下一篇: LeetCode Pancake Sor