动态转换:类和字符串进行比较

发布于 2024-09-06 23:56:39 字数 220 浏览 9 评论 0原文

不要尝试用语言来表达我的问题,这里有一些代码演示了我想要做的事情:

Class cls = Double.class
String str = "31.4";
Comparable comparableObj null;

comparableObj = (Comparable) cls.cast(str);

有什么想法吗?我已经考虑过使用反射但没有成功。

Instead of trying to trying to put my problem into words, here's some code that demonstrates what I want to do:

Class cls = Double.class
String str = "31.4";
Comparable comparableObj null;

comparableObj = (Comparable) cls.cast(str);

Any ideas? I've looked at using reflection but with no success.

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

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

发布评论

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

评论(2

轮廓§ 2024-09-13 23:56:39

我真的不喜欢你想要做的事情,但这是我对你的代码的修改,以便它编译并运行:

Class [] domains = { Integer.class, Double.class, String.class };
String [] stringValues = { "12", "31.4", "dog" };
Comparable [] comparableObjects = { null, null, null };

for (int i = 0; i < domains.length; i++) {
    Constructor con = domains[i].getConstructor(String.class);
    comparableObjects[i] = (Comparable) con.newInstance(stringValues[i]);
    System.out.println(comparableObjects[i]);
}

打印:

12
31.4
dog

如果你用文字解释你想要实现的目标,那么它可能会有所帮助,那么你可能会得到更多帮助以更好的方式做到这一点。

I really don't like what you are trying to do, but here is my modification of your code so that it compiles and runs:

Class [] domains = { Integer.class, Double.class, String.class };
String [] stringValues = { "12", "31.4", "dog" };
Comparable [] comparableObjects = { null, null, null };

for (int i = 0; i < domains.length; i++) {
    Constructor con = domains[i].getConstructor(String.class);
    comparableObjects[i] = (Comparable) con.newInstance(stringValues[i]);
    System.out.println(comparableObjects[i]);
}

Prints:

12
31.4
dog

It might help though if you explain in words what you are trying to achieve, then you might get more help doing it a better way.

灵芸 2024-09-13 23:56:39

您必须调用每个类的 valueOf 方法。所以也许:

for (int i = 0; i < domains.length; i++) {
    try {
        comparableObjects[i] = (Comparable) domains[i]
               .getMethod("valueOf", String.class).invoke(null, stringValues[i]);
    } catch (NoSuchMethodException ex) {
        comparableObjects[i] = stringValues[i];
    }
}

这段代码通过反射获取 valueOf 方法。 getMethod(..) 接受方法名称和参数类型,invoke(..) 接受 null 作为第一个参数,因为该方法是静态的。

如果您想要从字符串转换其他类,则必须使用它们的转换器方法。

但我不知道你是否真的需要这个,以及为什么。因为您知道所有的类和参数。

You would have to invoke the valueOf method of each of these classes. So perhaps:

for (int i = 0; i < domains.length; i++) {
    try {
        comparableObjects[i] = (Comparable) domains[i]
               .getMethod("valueOf", String.class).invoke(null, stringValues[i]);
    } catch (NoSuchMethodException ex) {
        comparableObjects[i] = stringValues[i];
    }
}

This code takes the valueOf method by reflection. getMethod(..) takes the method name and the argument type(s), and invoke(..) takes null as first argument, because the method is static.

If there are other classes that you want to convert from String, you'd have to use their converter methods.

But I don't know whether you actually need this, and why. Since you know all the classes and arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文