benleggiero
03/27/2017, 10:05 AMopen class Point<NumberType: Number>(
val x: NumberType,
val y: NumberType
)
open class Size<NumberType: Number>(
val width: NumberType,
val y: NumberType
)
abstract class Rect<NumberType: Number, PointType: Point<NumberType>, SizeType: Size<NumberType>>(
val origin: PointType,
val size: SizeType
) {
abstract constructor(x: NumberType, y: NumberType, width: NumberType, height: NumberType) // <== Constructor declared abstractly
val x get() = this.origin.x
val y get() = this.origin.y
val width get() = this.size.width
val height get() = this.size.height
open fun copy(
newOrigin: PointType = this.origin,
newSize: SizeType = this.size
): Rect<NumberType, PointType, SizeType>
= Rect(newOrigin, newSize)
open fun copy(
newX: NumberType = this.x,
newY: NumberType = this.y,
newWidth: NumberType = this.width,
newHeight: NumberType = this.height
): Rect<NumberType, PointType, SizeType>
= Rect(newX, newY, newWidth, newHeight)
}
class MyRect(origin: Point<Int>, size: Size<Int>): Rect<Int, Point<Int>, Size<Int>>(origin, size) {
override constructor(x: Int, y: Int, width: Int, height: Int): this(Point(x, y), Size(x, y)) // <== Constructor implemented/overridden
}
Full, non-SSCCE version:
https://github.com/BlueHuskyStudios/Blue-Base/blob/865d1330402eeb6f5d2b505a0a148df2ea46c4e7/JVM/src/org/bh/tools/base/math/geometry/Rect.kt#L19