Spark记录-Scala类与对象小例子

//基类-Person
class Person(val na: String, val ag: Int) {
  //属性
  var name: String = na
  var age: Int = ag
}
//派生类-sz,继承基类属性
class sz(override val na:String,override val ag:Int,val se:String,val add:String)
  extends Person(na,ag){
  //属性
  var sex:String=se
  var address:String=add
  //方法
  def print(): Unit ={
    println(name+"-"+age+"-"+sex+"-"+address)
  }
}
object study {
  def main(args: Array[String]):Unit= {
    try {
      val ps = new Person("深圳", 100); //定义一个基类对象
      println(ps.name + "-" + ps.age)//输出
      val szz = new sz("深圳", 100, "女", "广东省深圳市") //定义一个派生类对象
      print(szz.name + "-" + szz.age + "-" + szz.sex + "-" + szz.address + "\n") //输出
      szz.print() //调用派生类方法
    }
    catch {
      case e: Exception => e.printStackTrace
    }
    finally {
      println("finally")
    }
  }
}

  

posted @ 2017-11-16 10:26  信方  阅读(223)  评论(0编辑  收藏  举报