instanceof判断对象类型


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);
   }
}

猜你喜欢

转载自767148424.iteye.com/blog/2344236