62,621
社区成员
发帖
与我相关
我的任务
分享public class IsDouble {
public static boolean isDou(Number n){
boolean b=false;
try {
b=Class.forName("java.lang.Double").isInstance(n);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(!b){
try {
throw new MyException("is not a Double");
} catch (MyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return b;
}
public static void main(String[] args){
Double d=1.0;
int i=1;
System.out.println(isDou(d));
System.out.println(isDou(i));
}
}import java.lang.reflect.*;
class DoubleException extends Exception{
public String toString(){
return "It's not a double value!";
}
}
public class IsDouble{
double d = 1.0;
int j = 1;
public static void main(String args[]){
try{
Class c = Class.forName("IsDouble");
Field fieldlist[] = c.getDeclaredFields();
for (int i = 0; i < fieldlist.length; i++) {
Field fld = fieldlist[i];
if(fld.getType().getName().equals("double"))
throw new DoubleException();
else
System.out.println(fld.getName() +": "+ fld.getType());
}
}catch (Exception e) {
e.printStackTrace();
}
}
}It's not a double value!
at IsDouble.main(IsDouble.java:22)public class MyException extends Exception{
public MyException() {}
public MyException(String message) {
super(message);
}
}