Scala:如何获得mixin组合的类?
发布时间:2020-12-16 21:31:19 所属栏目:安全 来源:网络整理
导读:scala import java.util.Propertiesimport java.util.Propertiesscala trait Foo extends Propertiesdefined trait Fooscala classOf[Foo]res0: java.lang.Class[Foo] = interface Fooscala class FooP extends Foodefined class FooPscala classOf[FooP]res
scala> import java.util.Properties import java.util.Properties scala> trait Foo extends Properties defined trait Foo scala> classOf[Foo] res0: java.lang.Class[Foo] = interface Foo scala> class FooP extends Foo defined class FooP scala> classOf[FooP] res1: java.lang.Class[FooP] = class FooP scala> classOf[Properties with Foo] <console>:7: error: class type required but java.util.Properties with Foo found classOf[Properties with Foo] ^ scala> new Properties with Foo res2: java.util.Properties with Foo = {} scala> res2.getClass res3: java.lang.Class[_] = class $anon$1 有没有创建一个实例或新类的“Foo属性”类的方法? 解决方法
classOf [X]只有当X对应于物理类时才有效. T与U是复合类型,不对应于类.
您可以使用清单来确定类型的擦除. T与U的类型擦除是T. scala> trait T defined trait T scala> trait U defined trait U scala> manifest[T with U] res10: Manifest[T with U] = T with U scala> manifest[T with U].erasure res11: java.lang.Class[_] = interface T 在这里你可以看到List [Int]和List [_]有相同的擦除: scala> classOf[List[_]] res13: java.lang.Class[List[_]] = class scala.collection.immutable.List scala> classOf[List[Int]] res14: java.lang.Class[List[Int]] = class scala.collection.immutable.List scala> classOf[List[Int]] == classOf[List[_]] res15: Boolean = true (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |