类型不匹配错误声明类的列表 [英] type mismatch error declaring list of classes

查看:165
本文介绍了类型不匹配错误声明类的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  def getValueAndItsType() : List[ (AnyRef, Class[_]) ] = {
    val dataSet1 = ("some string data", classOf[String])
    val dataSet2 = (new Thread(), classOf[Thread])
    val dataSet3 = (new NullPointerException(), classOf[NullPointerException])
    val dataSet4 = (5, classOf[Int])
    val list = List(dataSet1, dataSet2, dataSet3, dataSet4)
    list
  }


b $ b






类型类型不匹配;发现:
List [(Any,Class [_>:Int with NullPointerException with Thread with
String])]必需:List [(AnyRef,Class [_])]

Type type mismatch; found : List[(Any, Class[_ >: Int with NullPointerException with Thread with String])] required: List[(AnyRef, Class[_])]







If dataSet4 is removed from List, the compile time error disappears




请注意,Class [_]有什么问题。是不是
相当于Java中的Class [?]我很感激,如果你还建议
正确的声明这样做..

Please suggest, what is wrong with Class[_]. Isn't it equivalent to Class[?] in java ? I appreciate, if you also suggest correct declaration for doing this..


推荐答案

Scala 中:


  • 任何 类类的根。
  • ,它延伸自 Any
  • AnyVal 所有值类型的类别。它延伸自任何

  • Null
  • / strong>包括 Null
  • Any is the root of the Scala class.
  • AnyRef is the root of the class of reference types, it extends from Any.
  • AnyVal is the root class of all value types. it extends from Any
  • Null is a subtype of all reference types.
  • Nothingis a subtype of all other types including Null

扩展任何,包括 AnyRef:引用类型 AnyVal:values类型

def getValueAndItsType() : List[ (Any, _ <: Any) ] = {
    val dataSet1 = ("some string data", classOf[String])
    val dataSet2 = (new Thread(), classOf[Thread])
    val dataSet3 = (new NullPointerException(), classOf[NullPointerException])
    val list = List(dataSet1, dataSet2, dataSet3)
    list
}

这篇关于类型不匹配错误声明类的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆