1.需求
定义一个具有一个参数的泛型类,在其中定义2个泛型方法,分别返回2个泛型形参的和与差,在main()函数中分别传入Double、Float、Integer类型实参,验证方法的执行2.我的代码
class BankBusiness<T>{
//加
public T plus(T num1,T num2){
return num1+num2; }
//减
public T minus(T num1,T num2){
return num1-num2; }
}public class Test { /**
* @param args
*/
public static void main(String[] args) {
BankBusiness<Double> bb1=new BankBusiness<Double>();
System.out.println(bb1.plus(1.2, 2.2)); }}3.背景a.作业题
b.百度无果
c.红色字体处提示出错,eclipse提示说未为参数类型T定义 + 操作4.求助
请问这个问题应该如何解决?请上级指示。5.贴尾
临帖涕零,不知所言。
定义一个具有一个参数的泛型类,在其中定义2个泛型方法,分别返回2个泛型形参的和与差,在main()函数中分别传入Double、Float、Integer类型实参,验证方法的执行2.我的代码
class BankBusiness<T>{
//加
public T plus(T num1,T num2){
return num1+num2; }
//减
public T minus(T num1,T num2){
return num1-num2; }
}public class Test { /**
* @param args
*/
public static void main(String[] args) {
BankBusiness<Double> bb1=new BankBusiness<Double>();
System.out.println(bb1.plus(1.2, 2.2)); }}3.背景a.作业题
b.百度无果
c.红色字体处提示出错,eclipse提示说未为参数类型T定义 + 操作4.求助
请问这个问题应该如何解决?请上级指示。5.贴尾
临帖涕零,不知所言。
return num1-num2;
public T plus(T num1,T num2);
public T minus(T num1,T num2);
}
定义成接口
然后Double、Float、Integer去实现这个接口
Double、Float、Integer 好像是final的吧。
public Double add(Double num1, Double num2) {
return num1+num2;
} @Override
public Double minus(Double num1, Double num2) {
return num1-num2;
}}interface BankBusiness<T>{
//加
public T add(T num1,T num2);
//减
public T minus(T num1,T num2);
}
public class Test { /**
* @param args
*/
public static void main(String[] args) {
MyDouble bb1=new MyDouble();
System.out.println(bb1.add(1.2, 2.2)); }}但是不太符合题目要求,感觉也是个问题。欢迎指点
* @author Redduke
*/
public class R<T extends Number> {
public T plus(T t1, T t2) {
Object v;
Class<?> clazz = t1.getClass();
if (clazz == Byte.class) {
v = (byte) (t1.byteValue() + t2.byteValue());
} else if (clazz == Short.class) {
v = (short) (t1.shortValue() + t2.shortValue());
} else if (clazz == Integer.class) {
v = t1.intValue() + t2.intValue();
} else if (clazz == Long.class) {
v = (long) (t1.longValue() + t2.longValue());
} else if (clazz == Float.class) {
v = (float) (t1.floatValue() + t2.floatValue());
} else if (clazz == Double.class) {
v = t1.doubleValue() + t2.doubleValue();
} else
throw new IllegalArgumentException();
return (T) v;
} public T minus(T t1, T t2) {
Object v;
Class<?> clazz = t1.getClass();
if (clazz == Byte.class) {
v = (byte) (t1.byteValue() - t2.byteValue());
} else if (clazz == Short.class) {
v = (short) (t1.shortValue() - t2.shortValue());
} else if (clazz == Integer.class) {
v = t1.intValue() - t2.intValue();
} else if (clazz == Long.class) {
v = (long) (t1.longValue() - t2.longValue());
} else if (clazz == Float.class) {
v = (float) (t1.floatValue() - t2.floatValue());
} else if (clazz == Double.class) {
v = t1.doubleValue() - t2.doubleValue();
} else
throw new IllegalArgumentException();
return (T) v;
} public static void main(String[] args) {
R<Double> rd = new R<Double>();
System.out.println(rd.plus(1.1, 1.2));
System.out.println(rd.minus(1.1, 1.2)); R<Float> rf = new R<Float>();
System.out.println(rf.plus(1.1f, 1.2f));
System.out.println(rf.minus(1.1f, 1.2f)); R<Integer> ri = new R<Integer>();
System.out.println(ri.plus(1, 2));
System.out.println(ri.minus(1, 2));
}
}
public class Operation<O > {
public O num;
public O add(O num1,O num2){
BigDecimal b1=new BigDecimal(num1.toString());
BigDecimal b2=new BigDecimal(num2.toString());
num=(O)b1.add(b2);
return num;
}
public O subtract(O num1,O num2){
BigDecimal b1=new BigDecimal(num1.toString());
BigDecimal b2=new BigDecimal(num2.toString());
num=(O)b1.subtract(b2);
return num;
}