package Test0817;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class Base{
int a;
Base(){
a=1;
System.out.println("Base Construct");
}
public void f(){
System.out.println("Base");
}
}
class Sub extends Base{
int a;
int b;
Sub(){
a=2;
b=2;
System.out.println("Sub Construct");
}
Sub(int i){
a=i;
b=i;
System.out.println("Sub Construct.this have one param");
}
public void f(){
System.out.println("Sub");
}
public void f(int m){
System.out.println("Sub,the m = "+m);
}
}
public class TestFanshe {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, SecurityException {
Class<?> c;
try {
c = Class.forName("Test0817.Sub"); //需要有完整的包名.类名
Sub s = (Sub) c.newInstance();//实例化
//得到构造函数
c.getConstructors();
//得到方法
Method method = c.getMethod("f");
System.out.println("the method is "+method.toString());
Class[] paramerClass = new Class[1];
paramerClass[0] = int.class; //这个地方要写int,与形参的参数类型一致,写Integer就会报方法找不到
Method method1 = c.getMethod("f", paramerClass);
System.out.println("the method is "+method1.toString());
//通过反射,调用s对象的方法
method.invoke(s); //无参数的
int a=10;
method1.invoke(s, a); //一个参数的
method1.invoke(s, 20);
//实例化,将父类引用指向
Base b = (Base) c.newInstance();
Base bs = (Sub) c.newInstance();
//Sub sb = (Base) c.newInstance(); 报错
s.f(); //输出 sub
b.f(); //输出 sub
//b.f(10);//报错,无法调用父类中没有的子类方法
bs.f(); //输出 sub
//bs.f(10);//报错,无法调用父类中没有的子类方法
} catch (ClassNotFoundException e) {
System.out.println("发生无该类异常");
e.printStackTrace();
}catch(NoSuchMethodException e){
System.out.println("发生无该方法异常");
e.printStackTrace();
}catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出结果:
Base Construct
Sub Construct
the method is public void Test0817.Sub.f()
the method is public void Test0817.Sub.f(int)
Sub
Sub,the m = 10
Sub,the m = 20
Base Construct
Sub Construct
Base Construct
Sub Construct
package Test0817; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; class Base{ int a; Base(){ a=1; System.out.println("Base Construct"); } public void f(){ System.out.println("Base"); } } class Sub extends Base{ int a; int b; Sub(){ a=2; b=2; System.out.println("Sub Construct"); } Sub(int i){ a=i; b=i; System.out.println("Sub Construct.this have one param"); } public void f(){ System.out.println("Sub"); } public void f(int m){ System.out.println("Sub,the m = "+m); } } public class TestFanshe { public static void main(String[] args) throws InstantiationException, IllegalAccessException, SecurityException { Class<?> c; try { c = Class.forName("Test0817.Sub"); //需要有完整的包名.类名 Sub s = (Sub) c.newInstance();//实例化 //得到构造函数 c.getConstructors(); //得到方法 Method method = c.getMethod("f"); System.out.println("the method is "+method.toString()); Class[] paramerClass = new Class[1]; paramerClass[0] = int.class; //这个地方要写int,与形参的参数类型一致,写Integer就会报方法找不到 Method method1 = c.getMethod("f", paramerClass); System.out.println("the method is "+method1.toString()); //通过反射,调用s对象的方法 method.invoke(s); //无参数的 int a=10; method1.invoke(s, a); //一个参数的 method1.invoke(s, 20); //实例化,将父类引用指向 Base b = (Base) c.newInstance(); Base bs = (Sub) c.newInstance(); //Sub sb = (Base) c.newInstance(); 报错 s.f(); //输出 sub b.f(); //输出 sub //b.f(10);//报错,无法调用父类中没有的子类方法 bs.f(); //输出 sub //bs.f(10);//报错,无法调用父类中没有的子类方法 } catch (ClassNotFoundException e) { System.out.println("发生无该类异常"); e.printStackTrace(); }catch(NoSuchMethodException e){ System.out.println("发生无该方法异常"); e.printStackTrace(); }catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }