scala面向对象编程

//scala 中简单的类的实例化调用

object test { def main(args: Array[String]): Unit
= { val hello=new student hello.increase() println(hello.fun()) } class student{ private var age=18 val name="stenven" def increase()=age+=1 def fun()=age } }

 

class student{
  private  var age=18
  private  var name= ""
  private var classNum=1

  def this(name:String){
    this()
    this.name=name
  }
  def this(name:String,classNum:Int){
    this(name)
    this.classNum=classNum
  }

  def  increase(step:Int)={age+=step}
  def current()={age}
  def info()={
    printf("name:%s and classNum is %d\n",name,classNum)
  }
}

object TestStudent_02{
  def main(args: Array[String]): Unit = {
    val myStudent1=new student
    val myStudent2=new student("zhang san")
    val myStudent3=new student("Lisi,75")
    myStudent1.info()
    myStudent1.increase(1)
    printf("11111111111Current age is: %d\n",myStudent1.current())
    println()


    myStudent2.info()
    myStudent2.increase(2)
    printf("2222222222222Current age is: %d\n",myStudent2.current())
   println()

    myStudent3.info()
    myStudent3.increase(2)
    printf("333333333333Current age is: %d\n",myStudent3.current())

  }
}

 

posted on 2019-08-01 15:40  床上小老虎  阅读(186)  评论(0)    收藏  举报

导航