1 Star 0 Fork 0

losidk/java_notes

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ReflectDemo4.java 724 Bytes
一键复制 编辑 原始数据 按行查看 历史
package com.test02;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
// 通过反射获取私有构造方法
public class ReflectDemo4 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> c1 = Class.forName("com.test01.Student");
Constructor<?> con = c1.getDeclaredConstructor(String.class);
// 暴力反射
con.setAccessible(true); // 取消访问检查(构造方法为 private 时无法使用该构造方法 实例化对象)
Object o = con.newInstance("林青霞");
System.out.println(o);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/losidk/java_notes.git
git@gitee.com:losidk/java_notes.git
losidk
java_notes
java_notes
master

搜索帮助