一个通用的加法计算,适合所有类型 注:不用方法重载实现
2009-01-07 12:09
302 查看
该方法实现的是加法运算。加法两边的数据可以是原始类型,或者是对原始类型的封装类型,甚至是一个class对象。
大家有什么想法或者是该算法有什么欠缺的,还望指教。
为此我们先写一个类,只为做演示,所有很简单。并重写其中的toString方法:
public class B {
public String toString(){
return "/"This is B/"";
}
}
下面是加法运算的具体实现:
public static Object cal(Object o1, Object o2) {
if ( !(o1 instanceof Number) || !(o2 instanceof Number) ) {
return o1.toString() + o2.toString();
} else {
//为产生最适合的大小的类型,从最大到最小写出来
if( (o1 instanceof Double) || o2 instanceof Double){
return (new Double(o1.toString()).doubleValue() + new Double(o2.toString()).doubleValue());
}else if( (o1 instanceof Float) || (o2 instanceof Float) ){
return (new Float(o1.toString()).floatValue() + new Float(o2.toString()).floatValue());
}else if( (o1 instanceof Long) || (o2 instanceof Long) ){
return (new Long(o1.toString()).longValue() + new Long(o2.toString()).longValue());
}else if( (o1 instanceof Integer) || (o2 instanceof Integer) ){
return (new Integer(o1.toString()).intValue() + new Integer(o2.toString()).intValue());
}else if( (o1 instanceof Short) || (o2 instanceof Short) ){
return (new Short(o1.toString()).shortValue() + new Short(o2.toString()).shortValue());
}else if( (o1 instanceof Byte) || (o2 instanceof Byte) ){
return (new Byte(o1.toString()).byteValue() + new Byte(o2.toString()).byteValue());
}else {
return (new Double(o1.toString()).doubleValue() + new Double(o2.toString()).doubleValue());
}
}
}
做几个测试例子:并输出结果:
(11.2,new B() )= 11.2"This is B"
(7,new B() )= 7"This is B"
(new B(),7 )= "This is B"7
(new B(),11.2 )= "This is B"11.2
(new B(),new B() )= "This is B""This is B"
(11.2,11.69874)= 22.89874
(11,11.69874)= 22.69874
(11.69874,11)= 22.69874
(11,7)= 18
short a = 9990; (a,7)= 9997
float b = 10.2f; (b,7)= 17.2
byte c = 10;; ( c,7)= 17
(11151515151510L,7)= 11151515151517
字符串56和数字7相加= 567
数字7和字符串56相加= 756
字符7和数字7相加= 77
字符7和字符0相加= 10
两个字符串11和0相加= 110
大家有什么想法或者是该算法有什么欠缺的,还望指教。
为此我们先写一个类,只为做演示,所有很简单。并重写其中的toString方法:
public class B {
public String toString(){
return "/"This is B/"";
}
}
下面是加法运算的具体实现:
public static Object cal(Object o1, Object o2) {
if ( !(o1 instanceof Number) || !(o2 instanceof Number) ) {
return o1.toString() + o2.toString();
} else {
//为产生最适合的大小的类型,从最大到最小写出来
if( (o1 instanceof Double) || o2 instanceof Double){
return (new Double(o1.toString()).doubleValue() + new Double(o2.toString()).doubleValue());
}else if( (o1 instanceof Float) || (o2 instanceof Float) ){
return (new Float(o1.toString()).floatValue() + new Float(o2.toString()).floatValue());
}else if( (o1 instanceof Long) || (o2 instanceof Long) ){
return (new Long(o1.toString()).longValue() + new Long(o2.toString()).longValue());
}else if( (o1 instanceof Integer) || (o2 instanceof Integer) ){
return (new Integer(o1.toString()).intValue() + new Integer(o2.toString()).intValue());
}else if( (o1 instanceof Short) || (o2 instanceof Short) ){
return (new Short(o1.toString()).shortValue() + new Short(o2.toString()).shortValue());
}else if( (o1 instanceof Byte) || (o2 instanceof Byte) ){
return (new Byte(o1.toString()).byteValue() + new Byte(o2.toString()).byteValue());
}else {
return (new Double(o1.toString()).doubleValue() + new Double(o2.toString()).doubleValue());
}
}
}
做几个测试例子:并输出结果:
(11.2,new B() )= 11.2"This is B"
(7,new B() )= 7"This is B"
(new B(),7 )= "This is B"7
(new B(),11.2 )= "This is B"11.2
(new B(),new B() )= "This is B""This is B"
(11.2,11.69874)= 22.89874
(11,11.69874)= 22.69874
(11.69874,11)= 22.69874
(11,7)= 18
short a = 9990; (a,7)= 9997
float b = 10.2f; (b,7)= 17.2
byte c = 10;; ( c,7)= 17
(11151515151510L,7)= 11151515151517
字符串56和数字7相加= 567
数字7和字符串56相加= 756
字符7和数字7相加= 77
字符7和字符0相加= 10
两个字符串11和0相加= 110
相关文章推荐
- 已知某公司总人数为W,平均年龄为Y岁(每年3月末计算,同时每年3月初入职新人),假设每年离职率为x,x>0&&x<1,每年保持所有员工总数不变进行招聘,新员工平均年龄21岁。 从今年3月末开始,请实现一个算法,可以计算出第N年后公司员工的平均年龄。(最后结果向上取整)。
- C#l练习(用方法来实现:①判断一个给定的整数是否为“质数”。②计算1-100之间的所有质数(素数)的和
- 实现表单中text类型取消其所有效果,让其成为一个无效果的text,并且实现只读效果
- 做一个通用的XML序列化,支持所有类型
- C语言实现的计算一个文件下的所有文件的总大小(还有BUG,如果文件夹下面还有文件家就有问题,等待更新)
- UNIX_C 环境下实现输入一个字符,不用回车直接输入功能(类型windows下_getch(void)函数)
- 练习2-1 编写一个程序一确定分别由signed及unsigned限定的char,short,int及long类型变量的取值范围。采用打印标准头文件中的相应值以及直接计算两种方式实现。通过直接计算来确定浮点类型的取值范围是一项难度很大的任务。
- 编写一个程序以确定分别由signed及unsigned限定的char、short、int及long类型变量的取值范围。采用打印标准头文件中的相应值以及直接计算两种方式实现
- 实现加法计算(不用加减乘除运算符)
- 【C语言】使用回调函数实现一个通用的冒泡排序,可以排序不同的数据类型。
- 使用回调函数实现一个通用的冒泡排序,可以排序不同的数据类型。
- 整数因子分解:计算一个整数所有的分解式(递归实现)
- 批量将一个库里的所有表里的char改成nchar类型
- 用原生JS实现一个简单计算的功能实例
- 用两个栈来实现一个队列完成队列的Push和Pop操作 队列中的元素为int类型。
- 汇编调用C语言 实现简单加法计算
- 求一个区间内所有的质数(C++实现)
- 【前端js】实现一个函数可以对JavaScript中的5种主要数据类型进行赋值
- 请实现一个函数能够计算一段文本中每个单词出现的次数。
- 批量将一个库里的所有表里的char改成nchar类型