import scala.reflect.ClassTag class IntStorage { var variable: Int = 5 } class testing[T : ClassTag] { var example = Array.ofDim[T](10, 10) def testFunc(): Int = example(0)(0).variable }
错误:值变量不是T的成员
这个 ClassTag 很好。但是你声明 testing 为之工作 课堂,不仅仅是 IntStorage . 如果我打电话怎么办 new testing[String]() ? String 是一个具有类标记的完全有效的类,但它没有 variable
ClassTag
testing
IntStorage
new testing[String]()
String
variable
class testing[T <: IntStorage : ClassTag] { ... }