Java 如何从私有静态内部类访问变量
我的班级结构如下:Java 如何从私有静态内部类访问变量,java,reflection,Java,Reflection,我的班级结构如下: public class Outer{ private Outer.Inner personal; public Outer(){ //processing. //personal assigned value } ........ private static class Inner { private final Set<String> innerPersonal;
public class Outer{
private Outer.Inner personal;
public Outer(){
//processing.
//personal assigned value
}
........
private static class Inner {
private final Set<String> innerPersonal;
Inner(){
innerPersonal=new HashSet<>();
//populate innerPersonal
}
}
}
公共类外部{
私人的外部的。内部的个人的;
公共图书馆{
//处理。
//个人赋值
}
........
私有静态类内部{
私人终场;
内(){
innerPersonal=新HashSet();
//填充内部个人
}
}
}
我的程序中有一个外部对象,
如何使用反射在程序中提取innerPersonal
@Retention(RetentionPolicy.RUNTIME)
public @interface Factory {
Class<?> value();
}
也许这是可行的。当你想在
外部执行代码时,你不能使用外部.internal.class
来引用你的静态内部类,因为它是私有的
,所以这里我提出了一种方法,它将首先获取字段personal
的值,然后调用getClass()
在字段的返回值上(假设它不是null
),以最终访问该内部类
,该类还允许访问其字段内部个人
Outer outer = ...
// Get the declared (private) field personal from the public class Outer
Field personalField = Outer.class.getDeclaredField("personal");
// Make it accessible otherwise you won't be able to get the value as it is private
personalField.setAccessible(true);
// Get the value of the field in case of the instance outer
Object personal = personalField.get(outer);
// Get the declared (private) field innerPersonal from the private static class Inner
Field innerPersonalField = personal.getClass().getDeclaredField("innerPersonal");
// Make it accessible otherwise you won't be able to get the value as it is private
innerPersonalField.setAccessible(true);
// Get the value of the field in case of the instance personal
Set<String> innerPersonal = (Set<String>)innerPersonalField.get(personal);
Outer=。。。
//从公共类获取声明的(私有)字段personal
Field personalField=Outer.class.getDeclaredField(“个人”);
//使其可访问,否则您将无法获取该值,因为它是私有的
personalField.setAccessible(true);
//获取实例外部的字段值
Object personal=personalField.get(外部);
//从私有静态类内部获取声明的(私有)字段innerPersonal
Field innerPersonalField=personal.getClass().getDeclaredField(“innerPersonal”);
//使其可访问,否则您将无法获取该值,因为它是私有的
innerPersonalField.setAccessible(true);
//获取实例personal的字段值
Set innerPersonal=(Set)innerPersonalField.get(personal);
您想在哪里从外部或其他类执行反射代码?我想在其他类中执行反射代码我在这个答案中看不到反射的任何用途。我无法更改外部类的结构抱歉,我没有看到您编写了“reflection”@user3781866
Outer o = new Outer();
Object r = o.getClass().getAnnotationsByType(Factory.class);
Outer outer = ...
// Get the declared (private) field personal from the public class Outer
Field personalField = Outer.class.getDeclaredField("personal");
// Make it accessible otherwise you won't be able to get the value as it is private
personalField.setAccessible(true);
// Get the value of the field in case of the instance outer
Object personal = personalField.get(outer);
// Get the declared (private) field innerPersonal from the private static class Inner
Field innerPersonalField = personal.getClass().getDeclaredField("innerPersonal");
// Make it accessible otherwise you won't be able to get the value as it is private
innerPersonalField.setAccessible(true);
// Get the value of the field in case of the instance personal
Set<String> innerPersonal = (Set<String>)innerPersonalField.get(personal);