Is there a way top make a subclass inherit the initialisation arguments of its parent class?
For example, If I can construct an object of class A
using new A(x=3,y=4)
and B
is a subclass of A
, I’d like to also contract a B as new B(x=3,y=4)
.
In Scala it seems to the the way to do that is to write a lot of boiler plating in the class definition of B
.
Here is what I’ve written. It seems like too much work and error prone for something so simple. I.e., I need to duplicate all the default values for every subclass, and I need to duplicate all the parameters with the extends EarthMap(...)
syntactical element. But all I really want to to is create several subclass which override two particular methods.
class PetersEarthMap(locationLL:Location=Location(-90,-180), // location on the globe in latitude;longitude
locationUR:Location=Location(90,180), // location on the globe in latitude;longitude
width:Int=360, height:Int=180, // size in pixels of the image file
borders:Boolean = true,
legend:Boolean = false,
palette:ColorPalette = ColorPalette.defaultTemperatureColorPalette)
extends EarthMap(locationLL=locationLL,locationUR=locationUR,
width=width,height=height,borders=borders,legend=legend,palette=palette) {
// peters projection
override def project(loc: Location): (Double, Double) = (90 * sin(toRadians(loc.lat)) -> loc.lon)
override def invProject(lat: Double, lon: Double): Location = Location(toDegrees(asin(lat / 90)), lon)
}
class SimpleEarthMap(locationLL:Location=Location(-90,-180), // location on the globe in latitude;longitude
locationUR:Location=Location(90,180), // location on the globe in latitude;longitude
width:Int=360, height:Int=180, // size in pixels of the image file
borders:Boolean = true,
legend:Boolean = false,
palette:ColorPalette = ColorPalette.defaultTemperatureColorPalette)
extends EarthMap(locationLL=locationLL,locationUR=locationUR,
width=width,height=height,borders=borders,legend=legend,palette=palette) {
// simple projection
def project(loc: Location):(Double,Double) = (loc.lat -> loc.lon)
def invProject(lat: Double, lon: Double):Location = Location(lat,lon)
}
abstract class EarthMap(locationLL:Location=Location(-90,-180), // location on the globe in latitude;longitude
locationUR:Location=Location(90,180), // location on the globe in latitude;longitude
width:Int=360, height:Int=180, // size in pixels of the image file
borders:Boolean=true,
legend:Boolean=false,
palette:ColorPalette = ColorPalette.defaultTemperatureColorPalette) {
val Location(latMin: Double, lonMin: Double) = locationLL
val Location(latMax: Double, lonMax: Double) = locationUR
// projection
def project(loc: Location):(Double,Double)
def invProject(lat: Double, lon: Double):Location
/// stuff omitted
}