instanceof判断对象类型
程序员文章站
2022-06-04 08:46:42
...
myobject instanceof ExampleClass 对象 是不是 某个类
package com.newer.cjl.api;
public class SuanShu<E> {//泛型
public E add(E a, E b) { //返回值类型为泛型 基本类型
if(a instanceof Integer){ //这是个类型
Integer t = (Integer)a+(Integer)b;
return (E)t;
}
if(a instanceof Double){
Double t = (Double)a+(Double)b;
return (E)t;
}
return null;
}
}
package com.newer.cjl.api;
public class Demo {
public static void main(String[] args) {
SuanShu<Double> sf = new SuanShu<Double>();
double t = sf.add(10.5, 20.5);
System.out.println(t);
}
}
public class demo{
public static void main(String[] args){
SuanShu<Integer> sd=new SuanShu<Integer>();
Integer i=sd.add(10,23);
System.out.println(i);
}
}
上一篇: 实验2 数据库及数据库表的建立
下一篇: misc设备