将动态参数值设置到Java Reflection的泛型方法中

发布于 2024-12-09 03:18:28 字数 603 浏览 3 评论 0原文

我是一名新的 java 开发人员,我想用反射来开发我的代码。 我有一个类调用 User:

我想将动态值传递给这 3 个方法,所以在 java 反射中我得到了一些代码,但我不明白为什么?

import .....

public class user
{
   private int id;
   private String name;
   private Date dob;

   public setID(int id)
   {
      this.id = id;
   }

   public setName(String name)
   {
      this.name = name;
   }

   public setDOB(Date dob)
   {
      this.dob = dob;
   }
}

Class cls = Class.forName("user");
Method[] methods = cls.getDeclearedMethod();
for(Method m : methods)
{
   Object[] args = new Object[1];
   args[0] = .....
   m.invoke(cls, args[0]);
}

I'm a new java developer, and I want to develop my code with reflection.
I have a class call User:

I want to pass dynamic value to those 3 methods, so in java reflection I got some code but I don't understand why?

import .....

public class user
{
   private int id;
   private String name;
   private Date dob;

   public setID(int id)
   {
      this.id = id;
   }

   public setName(String name)
   {
      this.name = name;
   }

   public setDOB(Date dob)
   {
      this.dob = dob;
   }
}

Class cls = Class.forName("user");
Method[] methods = cls.getDeclearedMethod();
for(Method m : methods)
{
   Object[] args = new Object[1];
   args[0] = .....
   m.invoke(cls, args[0]);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居者 2024-12-16 03:18:28

我不敢问你为什么要这样做......这样,但我希望这个例子可以帮助你感受Java提供的一些反射功能。

import java.lang.reflect.Method;
import java.util.Date;

public class Ref {

    public static void main(String[] args) throws ClassNotFoundException,
            InstantiationException, IllegalAccessException {
        Class cls = Class.forName("User");
        Object o = cls.newInstance();
        Object[] fieldValues = { new Integer(1), "", new Date() };
        Method[] methods = cls.getDeclaredMethods();

        for (Method m : methods) {
            Class[] paramTypes = m.getParameterTypes();
            Object[] paramValues = new Object[1];

            if (paramTypes.length == 0) {
                continue;
            }

            if (paramTypes[0].equals(Date.class)) {
                paramValues[0] = new Date();
            } else if (paramTypes[0].equals(String.class)) {
                paramValues[0] = "nice";
            } else if (paramTypes[0].equals(Integer.TYPE)) {
                paramValues[0] = 2;
            }

            if (paramValues[0] != null) {

                try {
                    m.invoke(o, paramValues[0]);
                } catch (Exception e) {

                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } // end for
        } // end for

        System.out.println("o = " + o);
    } // end method main
} // end class Ref

class User {
    private int id;
    private String name;
    private Date dob;

    public void setID(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDOB(Date dob) {
        this.dob = dob;
    }

    public String toString() {
        return "[id = " + id + ", name = " + name + ", date = " + dob + "]";
    }
}

I don't dare to ask why you wanna do this... this way but i hope this example helps you get the feeling of some of the capabilities of reflection provided by Java.

import java.lang.reflect.Method;
import java.util.Date;

public class Ref {

    public static void main(String[] args) throws ClassNotFoundException,
            InstantiationException, IllegalAccessException {
        Class cls = Class.forName("User");
        Object o = cls.newInstance();
        Object[] fieldValues = { new Integer(1), "", new Date() };
        Method[] methods = cls.getDeclaredMethods();

        for (Method m : methods) {
            Class[] paramTypes = m.getParameterTypes();
            Object[] paramValues = new Object[1];

            if (paramTypes.length == 0) {
                continue;
            }

            if (paramTypes[0].equals(Date.class)) {
                paramValues[0] = new Date();
            } else if (paramTypes[0].equals(String.class)) {
                paramValues[0] = "nice";
            } else if (paramTypes[0].equals(Integer.TYPE)) {
                paramValues[0] = 2;
            }

            if (paramValues[0] != null) {

                try {
                    m.invoke(o, paramValues[0]);
                } catch (Exception e) {

                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } // end for
        } // end for

        System.out.println("o = " + o);
    } // end method main
} // end class Ref

class User {
    private int id;
    private String name;
    private Date dob;

    public void setID(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDOB(Date dob) {
        this.dob = dob;
    }

    public String toString() {
        return "[id = " + id + ", name = " + name + ", date = " + dob + "]";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文