私有构造函数产生对象

原文链接
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class AccessHello {
	public static void main(String[] args) throws Exception {
		Class<?> c = Class.forName("jbbtlh.jbb.tlh.reflect.Hello");
		Constructor<?>[] con = c.getDeclaredConstructors();
		con[0].setAccessible(true);
		Object obj = con[0].newInstance();
		Method method = c.getDeclaredMethod("f");
		method.invoke(obj);
	}
}

class Hello {
	private Hello() {
	}

	public void f() {
		System.out.println("******");
	}
}




你可能感兴趣的:(java,C++,c,C#,asp)