TwoString.java
public class TwoString { private String m_s1, m_s2; public String getM_s1() { return m_s1; } public void setM_s1(String m_s1) { this.m_s1 = m_s1; } public String getM_s2() { return m_s2; } public void setM_s2(String m_s2) { this.m_s2 = m_s2; } public TwoString(String s1, String s2) { m_s1 = s1; m_s2 = s2; } }
ReflectConstructor.java
import java.lang.reflect.*; public class ReflectConstructor { public static void main(String[] args) throws Exception { Class[] types = new Class[] { String.class, String.class }; Constructor<TwoString> cons = TwoString.class.getConstructor(types); Object[] args1 = new Object[] { "a", "b" }; TwoString ts = cons.newInstance(args1); Field field = TwoString.class.getDeclaredField("m_s1"); field.setAccessible(true); System.out.println(field.get(ts)); } }
posted on 2010-11-05 09:21 java课程设计 阅读(180) 评论(0) 编辑 收藏 举报