亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

scala怎么定義無參構造函數

scala怎么定義無參構造函數

富國滬深 2019-04-12 10:06:20
scala怎么定義無參構造函數
查看完整描述

2 回答

?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

類名后面沒有參數就是無參的:

1

2

3

4

5

class mytest {       

  val i=100

  val s="my name"

}

val m=new mytest

有參數,但是每個參數都有默認值的構造器,也可以看成支持無參的

也可以寫一個輔助構造器,是無參的

1

2

3

4

5

class mytest2(val x:Int) {

  val s="my name"

  def  this()  = this(100)

}

val m2 = new mytest2

   

 


查看完整回答
反對 回復 2019-04-13
?
largeQ

TA貢獻2039條經驗 獲得超8個贊

直接寫到類的里面就可以了:

scala> class A {
| //do anything you want
| println("Hi, the construtor invoked here..")
| }
defined class A

scala> val a = new A
Hi, the construtor invoked here..
a: A = A@1339a0dc

scala>

上面的Scala代碼基本等價于下面的Java代碼:

public class A {
public A() {
//do anything you want
println("Hi, the construtor invoked here..");
}
}



查看完整回答
反對 回復 2019-04-13
  • 2 回答
  • 0 關注
  • 1983 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號