码迷,mamicode.com
首页 > 其他好文 > 详细

Scala-构造函数

时间:2019-02-13 14:20:24      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:poi   pac   rgs   函数   pre   new   this   tostring   def   

package com.bigdata

/*
scala的构造函数分为主构造函数和辅助构造函数。

一、主构造函数
在Scala中,每个类都有主构造函数,和类的定义交织在一起。
一个Scala类的主构造函数包括:1、构造函数参数;2、在类内部被调的方法;3、在类内部执行的语句和表达式。
*/


class Person(var firstName : String, var lastName : String) { //构造函数参数

println("the constructor begins")

var age = 0

override def toString = s"$firstName $lastName is $age years old"

def printFullName {print(this)}

printFullName //被调的方法

println("still in the constructor")

}

object Person{
def main(args: Array[String]): Unit = {

val big = new Person("Poison", "Pink")

}
}

Scala-构造函数

标签:poi   pac   rgs   函数   pre   new   this   tostring   def   

原文地址:https://www.cnblogs.com/shimingjie/p/10369421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!