方法想实现的功能就是传入子类返回父类的对象
public static S getSuperEntity(T t) {
Method s = null;
Class> classType = t.getClass().getSuperclass();
System.out.println(classType);
S d = null;
Method m = null;
Method m1 = null;
try {
//问题在这,我要怎么给这个实例化啊。可不可以实现
d = Class.forName(classType.getName()).newInstance();
Field[] f = classType.getDeclaredFields();
// Method[] m = d.getClass().getDeclaredMethods();
for (Field fs : f) {
m = d.getClass().getDeclaredMethod(
"set"
+ fs.getName().substring(0, 1).toUpperCase()
+ fs.getName().substring(1,
fs.getName().length()), String.class);
m1 = t.getClass().getDeclaredMethod(
"get"
+ fs.getName().substring(0, 1).toUpperCase()
+ fs.getName().substring(1,
fs.getName().length()), String.class);
m.invoke(d, m1.invoke(t));
System.out.println(fs.getName());
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return d;
}
这样写报错
class com.classTest.Person
java.lang.InstantiationException: com.classTest.Person
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at com.classTest.MainTest.getSuperEntity(MainTest.java:28)
at com.classTest.MainTest.main(MainTest.java:15)
Exception in thread "main" java.lang.NullPointerException
at com.classTest.MainTest.main(MainTest.java:16)
求指教!!!