scala怎么定义无参构造函数

scala怎么定义无参构造函数

动漫人物
浏览 774回答 2
2回答

烙印99

类名后面没有参数就是无参的:12345class mytest {         val i=100  val s="my name"}val m=new mytest有参数,但是每个参数都有默认值的构造器,也可以看成支持无参的也可以写一个辅助构造器,是无参的12345class mytest2(val x:Int) {  val s="my name"  def  this()  = this(100)}val m2 = new mytest2    

陪伴而非守候

直接写到类的里面就可以了:scala> class A {| //do anything you want| println("Hi, the construtor invoked here..")| }defined class Ascala> val a = new AHi, the construtor invoked here..a: A = A@1339a0dcscala>上面的Scala代码基本等价于下面的Java代码:public class A {public A() {//do anything you wantprintln("Hi, the construtor invoked here..");}}
打开App,查看更多内容
随时随地看视频慕课网APP