[关闭]
@student2018 2018-10-20T01:56:59.000000Z 字数 590 阅读 452

Scala 练习2

Scala


the more you know about scala type system, the more you can give the scala compiler.

  1. package student.scala
  2. import scala.reflect.ClassTag
  3. object Execise2 extends App {
  4. //1 def foo (msg:String) = println(msg)
  5. //改造上面的这个函数,使其可以输出
  6. //foo(100) foo(List(1,2,3)
  7. // foo(Array(1,2,3) :Array[1,2,3]
  8. def foo(msg:String) = println(msg)
  9. foo("abc")
  10. def foo2[T](msg:T) = msg match { case a:Array[_]=> println("Array[" + a.mkString(",") + "]") ;case _=> println(msg.toString()) }
  11. foo2(List(1,2,3))
  12. foo2(100)
  13. foo2(Array(1,2,3))
  14. //2 写一个泛型函数,接受一个Array,
  15. //然后返回一个仅包含原数组第一个元素的新数组
  16. def first[T:ClassTag](a:Array[T]) = Array(a(0))
  17. println(first(Array(1,2,3)).mkString(""))
  18. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注