B
benoit.isaac
Hello all,
I am experiencing a problem trying to use the generics feature of Java
1.5.
Here it is:
- I have a Generic class Measure that is parameterized by any kind of
number:
public class Measure<U extends Number>{
private String name;
public Measure(String _name)
{
name = _name;
}
.....
}
- In my program, I want my measures to be either Float or Double, but I
would like to have to set this only in one place in the program.
So I would like to rename (ie, create a shortcut to) a Measure
parameterized. That is, I would like to do something like:
public class MeasureForce extends Measure<Double> {}
so that I can use everywhere : MeasureForce f = new MeasureForce()...
and if I want to switch to Float numbers, I just change the definition
of MeasureForce in one class file to :
public class MeasureForce extends Measure<Float> {}
Unfortunately, this doesn't seem to work. I can create the MeasureForce
class but then I cannot use the functions defined in Measure<U>, that
is, a call to : MeasureForce f = new MeasureForce("nameForce") gives me
an error saying that the "MeasureForce(String) constructor is not
defined".
How can I do what I would like to do ?
I also something like using Measure<UnitForce> where UnitForce would
extend Double, unfortunately it is not possible to extend Double nor
Float (final classes).
Thank you very much for any help on this,
Kind regards
Benoit
I am experiencing a problem trying to use the generics feature of Java
1.5.
Here it is:
- I have a Generic class Measure that is parameterized by any kind of
number:
public class Measure<U extends Number>{
private String name;
public Measure(String _name)
{
name = _name;
}
.....
}
- In my program, I want my measures to be either Float or Double, but I
would like to have to set this only in one place in the program.
So I would like to rename (ie, create a shortcut to) a Measure
parameterized. That is, I would like to do something like:
public class MeasureForce extends Measure<Double> {}
so that I can use everywhere : MeasureForce f = new MeasureForce()...
and if I want to switch to Float numbers, I just change the definition
of MeasureForce in one class file to :
public class MeasureForce extends Measure<Float> {}
Unfortunately, this doesn't seem to work. I can create the MeasureForce
class but then I cannot use the functions defined in Measure<U>, that
is, a call to : MeasureForce f = new MeasureForce("nameForce") gives me
an error saying that the "MeasureForce(String) constructor is not
defined".
How can I do what I would like to do ?
I also something like using Measure<UnitForce> where UnitForce would
extend Double, unfortunately it is not possible to extend Double nor
Float (final classes).
Thank you very much for any help on this,
Kind regards
Benoit