W
Webb Roberts
I'm having trouble ironing out language features in 1.5 to perform some
operations and have them be strongly typed. I'd like to implement
strongly-typed classes derived from a simple base class. It seems like
the result would be a combination of covariant return types, plus a
proper use of <T extends Number>, but I can't seem to get it right.
public abstract class Number {
public abstract Number add(Number number);
}
The derived classes should override add(n), but be strongly typed.
Something that acts like what follows:
public class RealNumber extends Number {
private final double real;
public RealNumber(double real) {
this.real = real;
}
public RealNumber add(RealNumber rhs) {
return new RealNumber(this.real + rhs.real);
}
}
But, obviously, you'd want to be able to do operations in a proper
generic way, like on a Vector<T extends Number>.
Another example, for complex numbers:
public class ComplexNumber extends Number {
private final double real;
private final double imaginary;
public RealNumber(double real, double imaginary) {
this.real = real;
this.imaginary = real;
}
public ComplexNumber add(ComplexNumber rhs) {
return new ComplexNumber(this.real + rhs.real, this.imaginary +
rhs.imaginary);
}
}
operations and have them be strongly typed. I'd like to implement
strongly-typed classes derived from a simple base class. It seems like
the result would be a combination of covariant return types, plus a
proper use of <T extends Number>, but I can't seem to get it right.
public abstract class Number {
public abstract Number add(Number number);
}
The derived classes should override add(n), but be strongly typed.
Something that acts like what follows:
public class RealNumber extends Number {
private final double real;
public RealNumber(double real) {
this.real = real;
}
public RealNumber add(RealNumber rhs) {
return new RealNumber(this.real + rhs.real);
}
}
But, obviously, you'd want to be able to do operations in a proper
generic way, like on a Vector<T extends Number>.
Another example, for complex numbers:
public class ComplexNumber extends Number {
private final double real;
private final double imaginary;
public RealNumber(double real, double imaginary) {
this.real = real;
this.imaginary = real;
}
public ComplexNumber add(ComplexNumber rhs) {
return new ComplexNumber(this.real + rhs.real, this.imaginary +
rhs.imaginary);
}
}