package com.lhj.scala import java.io.File import scala.io.Source object Test { abstract class A[T] { def add(x:T,y:T):T } abstract class B[T] extends A[T]{ def unit:T } def main(args: Array[String]): Unit = { implicit object StringAdd extends B[String]{ override def add(x:String,y:String) = x concat y override def unit:String = "" } implicit object IntAdd extends B[Int]{ override def add(x:Int,y:Int) = x + y override def unit:Int = 0 } def sum[T](x:List[T])(implicit b:B[T]): T = if (x.isEmpty) b.unit else b.add(x.head, sum(x.tail)) println(sum(List(1,2,3,4,5))) println(sum(List("Spark","Hadoop","Akka"))) } } --结果: 15 SparkHadoopAkka package com.lhj.scala import java.io.File import scala.io.Source object Test { def main(args: Array[String]): Unit = { val arr=Array(1,2,3,4) println(arr.head) println("---------------") for(x<-arr.tail){println(x)} } } --结果: 1 --------------- 2 3 4