L
leung.january
I have written my own Arithmetic class to do multiplication of
subclasses of Number and char, etc. But I am not sure how to make
multiply() invoke mult() of the right signature. Would I have to go
so far as to use Java reflection method such as getConstuctor() in
multiply()? Or is there a simpler way?
My current code has the following compilation error in the body of
multiply():
The method mult(Integer, Integer) in the type Arithmetic is not
applicable for the arguments (Object,
Object)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class Arithmetic {
public static Integer mult(Integer i, Integer j) {
return i * j;
}
public static Double mult(Double i, Double j) {
return i * j;
}
public static int mult(char i, char j) {
return ((int) i * (int) j);
}
// ... mult() on other data types
public static Object multiply(Object i, Object j) {
return mult(i, j);
}
public static void main(String[] args) {
System.out.println("3 * 4 = " + Arithmetic.mult((Integer) 3,
(Integer) 4));
System.out.println(Arithmetic.multiply(new Integer(1), new
Integer(3)));
}
}
subclasses of Number and char, etc. But I am not sure how to make
multiply() invoke mult() of the right signature. Would I have to go
so far as to use Java reflection method such as getConstuctor() in
multiply()? Or is there a simpler way?
My current code has the following compilation error in the body of
multiply():
The method mult(Integer, Integer) in the type Arithmetic is not
applicable for the arguments (Object,
Object)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class Arithmetic {
public static Integer mult(Integer i, Integer j) {
return i * j;
}
public static Double mult(Double i, Double j) {
return i * j;
}
public static int mult(char i, char j) {
return ((int) i * (int) j);
}
// ... mult() on other data types
public static Object multiply(Object i, Object j) {
return mult(i, j);
}
public static void main(String[] args) {
System.out.println("3 * 4 = " + Arithmetic.mult((Integer) 3,
(Integer) 4));
System.out.println(Arithmetic.multiply(new Integer(1), new
Integer(3)));
}
}