我有一个扩展Vec3t的Vec3i类
data class Vec3i(
override var x: Int = 0,
override var y: Int = 0,
override var z: Int = 0
) : Vec3t(x, y, z)
具有如下的一个辅助构造函数
constructor(v: Vec3t<Number>) : this(v.x.toInt(), v.y.toInt(), v.z.toInt())
另一类Vec3ub总是扩展Vec3t
data class Vec3ub(
override var x: Ubyte = Ubyte(0),
override var y: Ubyte = Ubyte(0),
override var z: Ubyte = Ubyte(0)
) : Vec3t(x, y, z)
反过来Vec3t的地方
abstract class Vec3t<T : Number>(
override var x: T,
override var y: T,
open var z: T
) : Vec2t(x, y)
Ubyte扩展了Number
我想从Vec3ub中实例化一个Vec3i
Vec3i(vec3ub)
但是编译器抱怨说没有构造函数 .
为什么我之前引用的辅助构造函数无效?
1 回答
为了完整性,如我的评论中所述,以下内容正确编译:
请注意
constructor(v : Vec3t<out Number>) : ...
和所有其他添加的通用参数类型 .Vec3t<out Number>
是必要的,而不仅仅是Vec3t<Number>
,因为你没有传递Number
,而是传递它的子类 .