欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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);
}
}
相关标签: java JVM