日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Scala 类

發(fā)布時間:2024/4/17 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Scala 类 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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
class HelloWorld {var age = 0var 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規(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ù)直接放置在類名之后:

import scala.beans.BeanPropertyclass 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.主構造器會執(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)定義的輔助構造器的調用開始。

class Person3 {private var name = ""private var age = 0def this(name: String) { // An auxiliary constructorthis() // Calls primary constructorthis.name = name}def this(name: String, age: Int) { // Another auxiliary constructorthis(name) // Calls previous auxiliary constructorthis.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 constructorp11.description p21.description p31.description

輔助構造器必須先調用主構造器,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

總結

以上是生活随笔為你收集整理的Scala 类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。