楼主,完全是可以的,你试试我这段代码- package com.itcast.io.day2;
- import java.lang.reflect.Method;
- import java.util.HashSet;
- import java.util.Iterator;
- class ReflectDemo {
- private void printStr() {
- System.out.println("test reflect");
- }
- public void printInt() {
- System.out.println(10);
- }
- }
- public class Test {
- public static void main(String[] args) throws Exception {
- Class cls = Class.forName("com.itcast.io.day2.ReflectDemo");
- Method method = cls.getDeclaredMethod("printStr");
- method.setAccessible(true);
- method.invoke(cls.newInstance());
- }
- }
复制代码 |