有没有办法动态传递类类型。
请考虑一下:
cool_project.my_package;
class ImportClass {
public void importClassMethod() {
// Do Things
}
}
然后:
class OtherClass {
private String fullClassPath = "cool_project.my_package.ImportClass";
private void otherClassMethod() {
Class myClass = Class.forName(fullClassPath);
Constructor constructor;
try {
constructor = myClass.getConstructor(Context.class);
cool_project.my_package.ImportClass importClass = (cool_project.my_package.ImportClass) constructor.newInstance(mContext);
importClass.importClassMethod();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
是否可以:
/* Get Type From fullClassPath */ importClass = (/* Get Type From fullClassPath */) constructor.newInstance(mContext);