本帖最后由 喝龙血的我 于 2013-10-11 20:46 编辑
- <p>public class Test {</p><p> public static void main(String []args) throws Exception{</p><p>
- //反射调用类P的初始化方法P
- Method pp=P.class.getMethod("P", Person.class);
- System.out.println(pp.toString());
-
- // P p=new P("abc",34);
- // System.out.println(p);
- // Method methodname=p.getClass().getMethod("setSs",String.class);
- // methodname.invoke(p, "def");
- // System.out.println(p);
-
- }
-
- }</p><p>class P{
- private String ss;
- public String getSs() {
- return ss;
- }
- public void setSs(String ss) {
- this.ss = ss;
- }
- public int getIi() {
- return ii;
- }
- public void setIi(int ii) {
- this.ii = ii;
- }
- private int ii;
- public P(String ss, int ii) {
- super();
- this.ss = ss;
- this.ii = ii;
- }</p><p> @Override
- public String toString() {
- return "P [ss=" + ss + ", ii=" + ii + "]";
- }
-
- }</p><p>怎么才能得到类P的初始化方法呢,为什么能得到get,set方法,却得不到初始化方法?</p>
复制代码 |
|