- package com.a;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- class Str{
- private String s;
- public Str(String s){
- this.s=s;
-
- }
- public void print(){
- System.out.println(s);
- }
- }
- public class Test3 {
-
- public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
- Class str=Str.class;
- Constructor cs=str.getConstructor(String.class);
- Str st=(Str) cs.newInstance("谢谢苏老师给分");
- Method m=str.getMethod("print" );
- m.invoke(st);
-
- }
-
- }
复制代码 |