请教:关于泛型的反射 如何用反射来改变下列代码中的"bbbList" 成员的值?class aaa { private List <bbbb> bbbList;}class bbb { private String aaa;} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.beans.IntrospectionException;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.util.ArrayList;import java.util.List;public class TestBean{ private String name; private int age; private boolean merrage; public List<Sub> list; public List<Sub> getList() { return list; } public void setList(List<Sub> list) { this.list = list; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean isMerrage() { return merrage; } public void setMerrage(boolean merrage) { this.merrage = merrage; } public String getName() { return name; } public void setName(String name) { this.name = name; } public static void main(String[] args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchFieldException { TestBean tb=new TestBean(); tb.setName("huangfeihong"); tb.setAge(70); tb.setMerrage(false); List<Sub> list=new ArrayList<Sub>(); Sub s1=new Sub(); Sub s2=new Sub(); s1.setName("sub1"); s2.setName("sub2"); list.add(s1); list.add(s2); tb.setList(list); System.out.println("Before, the list's size is: "+tb.getList().size()); Field myList=tb.getClass().getField("list"); List<Sub> myListValue=(List<Sub>)myList.get(tb); Sub s3=new Sub(); s3.setName("sub3"); myListValue.add(s3); myList.set(tb, myListValue); System.out.println("After, the list's size is: "+tb.getList().size()); }}class Sub{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }这样就可以了,但是这个是你的成员变量list必须是public的。 另外,如果你想保持成员变量的私有限制,可以使用java.beans中的API,这个时候你的类必须符合javaBean规范,也就是对于想操作的成员变量,比如有相应的get/set方法,如下:import java.beans.IntrospectionException;import java.lang.reflect.InvocationTargetException;import java.util.ArrayList;import java.util.List;public class TestBean{ private String name; private int age; private boolean merrage; private List<Sub> list; public List<Sub> getList() { return list; } public void setList(List<Sub> list) { this.list = list; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean isMerrage() { return merrage; } public void setMerrage(boolean merrage) { this.merrage = merrage; } public String getName() { return name; } public void setName(String name) { this.name = name; } public static void main(String[] args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchFieldException { TestBean tb=new TestBean(); tb.setName("huangfeihong"); tb.setAge(70); tb.setMerrage(false); List<Sub> list=new ArrayList<Sub>(); Sub s1=new Sub(); Sub s2=new Sub(); s1.setName("sub1"); s2.setName("sub2"); list.add(s1); list.add(s2); tb.setList(list); System.out.println("Before, the list's size is: "+tb.getList().size()); java.beans.BeanInfo bi=java.beans.Introspector.getBeanInfo(tb.getClass()); java.beans.PropertyDescriptor[] pds=bi.getPropertyDescriptors(); for(int i=0;i<pds.length;i++) { if(pds[i].getName().equals("list")) { List<Sub> theList=(List<Sub>)pds[i].getReadMethod().invoke(tb, new Object[]{}); Sub s3=new Sub(); s3.setName("sub3"); theList.add(s3); pds[i].getWriteMethod().invoke(tb, new Object[]{theList}); } System.out.println(pds[i].getName()+" : "+pds[i].getReadMethod().invoke(tb, new Object[]{})); } }}class Sub{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } 正则表达式 IP java 项目编译 exe 对于添加的外部jar,工程只能访问它的public class吗???? 键盘监听的问题 请教awt包和Swing包中的paint(),repaint(),update()问题 一个关于两次使用rs的问题 怎样才能使frame不能左右缩放,只能上下缩放 大家帮我看看2个问题,很简单的,谢谢! == 和 equals() 有什么异同? 谁能告诉我从技术上讲Window XP 不“支持”java编程是什么意思? idltojava哪里有下? 讨论一个专业的问题
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;public class TestBean
{
private String name;
private int age;
private boolean merrage;
public List<Sub> list;
public List<Sub> getList()
{
return list;
}
public void setList(List<Sub> list)
{
this.list = list;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public boolean isMerrage()
{
return merrage;
}
public void setMerrage(boolean merrage)
{
this.merrage = merrage;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public static void main(String[] args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchFieldException
{
TestBean tb=new TestBean();
tb.setName("huangfeihong");
tb.setAge(70);
tb.setMerrage(false);
List<Sub> list=new ArrayList<Sub>();
Sub s1=new Sub();
Sub s2=new Sub();
s1.setName("sub1");
s2.setName("sub2");
list.add(s1);
list.add(s2);
tb.setList(list);
System.out.println("Before, the list's size is: "+tb.getList().size());
Field myList=tb.getClass().getField("list");
List<Sub> myListValue=(List<Sub>)myList.get(tb);
Sub s3=new Sub();
s3.setName("sub3");
myListValue.add(s3);
myList.set(tb, myListValue);
System.out.println("After, the list's size is: "+tb.getList().size());
}
}class Sub
{
private String name; public String getName()
{
return name;
} public void setName(String name)
{
this.name = name;
}
}这样就可以了,但是这个是你的成员变量list必须是public的。
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;public class TestBean
{
private String name;
private int age;
private boolean merrage;
private List<Sub> list;
public List<Sub> getList()
{
return list;
}
public void setList(List<Sub> list)
{
this.list = list;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public boolean isMerrage()
{
return merrage;
}
public void setMerrage(boolean merrage)
{
this.merrage = merrage;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public static void main(String[] args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchFieldException
{
TestBean tb=new TestBean();
tb.setName("huangfeihong");
tb.setAge(70);
tb.setMerrage(false);
List<Sub> list=new ArrayList<Sub>();
Sub s1=new Sub();
Sub s2=new Sub();
s1.setName("sub1");
s2.setName("sub2");
list.add(s1);
list.add(s2);
tb.setList(list);
System.out.println("Before, the list's size is: "+tb.getList().size());
java.beans.BeanInfo bi=java.beans.Introspector.getBeanInfo(tb.getClass());
java.beans.PropertyDescriptor[] pds=bi.getPropertyDescriptors();
for(int i=0;i<pds.length;i++)
{
if(pds[i].getName().equals("list"))
{
List<Sub> theList=(List<Sub>)pds[i].getReadMethod().invoke(tb, new Object[]{});
Sub s3=new Sub();
s3.setName("sub3");
theList.add(s3);
pds[i].getWriteMethod().invoke(tb, new Object[]{theList});
}
System.out.println(pds[i].getName()+" : "+pds[i].getReadMethod().invoke(tb, new Object[]{}));
}
}
}class Sub
{
private String name; public String getName()
{
return name;
} public void setName(String name)
{
this.name = name;
}
}