如果要用來判斷傳入的是那種型態,參考如下 :
public class InstanceoftTest {
InstanceoftTest(){
String s = "this is String";
Integer i = new Integer(1);
BigDecimal b = new BigDecimal(2.34);
checkWithInstanceof(s);
checkWithInstanceof(i);
checkWithInstanceof(b);
}
public static void checkWithInstanceof(Object o){
if(o instanceof Integer){
System.out.println("Your Class Type is Integer");
}else if(o instanceof BigDecimal){
System.out.println("Your Class Type is BigDecimal");
}else{
System.out.println("Your Class Type will conver String");
}
}
}
public class InstanceoftTest {
InstanceoftTest(){
String s = "this is String";
Integer i = new Integer(1);
BigDecimal b = new BigDecimal(2.34);
checkWithInstanceof(s);
checkWithInstanceof(i);
checkWithInstanceof(b);
}
public static void checkWithInstanceof(Object o){
if(o instanceof Integer){
System.out.println("Your Class Type is Integer");
}else if(o instanceof BigDecimal){
System.out.println("Your Class Type is BigDecimal");
}else{
System.out.println("Your Class Type will conver String");
}
}
}