"java"
package com.hdj.core;import java.lang.reflect.*;import com.hdj.bean.Person;@SuppressWarnings("unused")
public class ServiceCore {
Method moth = null;
Class c = null;
Object Result = null;

public Object Service(String ClassName, String MothName, Object[]args) throws Exception{
c = Class.forName(ClassName);
Object o = c.newInstance();
Class[] arg = null;
for(int i=1; i<args.length; i++)
{
arg[i] = args[i].getClass();
}
moth = c.getMethod(MothName, arg);//此处报错
Result = moth.invoke(o, args);
return Result;
}
}"java"
package com.hdj.bean;public class Person {

private String name = null;
private String Sex = null;
private int age = 0;
private String Address = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println(getName());
}
public String getSex() {
return Sex;
}
public void setSex(String sex) {
Sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}

public String getInfo(){
return "Name: " + this.getName() + "\nSex: "
+ this.getSex() + "\nAge: " + this.getAge()
+ "\nAdress" + this.getAddress();
}
}
"java"
package com.hdj.reflect;import com.hdj.bean.Person;
import com.hdj.core.*;public class MyJReflect{
public static void main(String[] args) throws Exception {
ServiceCore s = new ServiceCore();
Person p = new Person();
s.Service(p.getClass().getName(), "setName", new Object[]{"HuDaoJun"});
}
}
ServiceCore类中的下面代码报错(参数传不进去)moth = c.getMethod(MothName, arg);这句报错:
Exception in thread "main" java.lang.NoSuchMethodException: com.hdj.bean.Person.setName()
at java.lang.Class.getMethod(Unknown Source)
at com.hdj.core.ServiceCore.Service(ServiceCore.java:21)
at com.hdj.reflect.MyJReflect.main(MyJReflect.java:10)
我想根据类名,方法名,参数列表,三个参数动态调用方法。
请问应该怎么实现?

解决方案 »

  1.   

    Class[] arg = new Class[args.length];//此处为null
    for(int i=0; i<args.length; i++)//此处从0开始
    {
         arg[i] = args[i].getClass();
    }
      

  2.   

    package com.g.c;
    import java.lang.reflect.*;
    public class MyJReflect {
    public static void main(String[] args) throws Exception {
            ServiceCore s = new ServiceCore();
            Person p = new Person();
            s.Service(p.getClass().getName(), "setName", new Object[]{"HuDaoJun"});
        }}
    class Person {
        
        private String name = null;
        private String Sex = null;
        private int age = 0;
        private String Address = null;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
            System.out.println(getName());
        }
        public String getSex() {
            return Sex;
        }
        public void setSex(String sex) {
            Sex = sex;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getAddress() {
            return Address;
        }
        public void setAddress(String address) {
            Address = address;
        }
        
        public String getInfo(){
            return "Name: " + this.getName() + "\nSex: "
                    + this.getSex() + "\nAge: " + this.getAge()
                    + "\nAdress" + this.getAddress();
        }
    }class ServiceCore {
        Method moth = null;
        Class c = null;
        Object Result = null;
        
        public Object Service(String ClassName, String MothName, Object[]args) throws Exception{
            c = Class.forName(ClassName);
            Object o = c.newInstance();
            //System.out.println(o.getClass());
            Class[] arg = new Class[args.length];
            for(int i=0; i<args.length; i++)
            {
                arg[i] = args[i].getClass();
            }
            //c.get
            moth = c.getMethod(MothName, arg);//此处报错[/color]
            Result = moth.invoke(o, args);        
            return Result;
        }
    }