Java 中的动态转换

发布于 2024-10-11 00:28:52 字数 278 浏览 0 评论 0原文

我如何概括以下代码:

String nameString = "myClass_t";
myClass_t myVar = (myClass_t) AnotherClass.classMethod();

AnotherClass.classMethod() 的返回类型需要从文件中获取(因此是一个 String 变量)。 myVar 的类型需要与该类型匹配。

如何重写第二行以使其能够从 nameString 获取类型?

非常感谢您的帮助。

How can I generalize the following code:

String nameString = "myClass_t";
myClass_t myVar = (myClass_t) AnotherClass.classMethod();

The return type of AnotherClass.classMethod() needs to be obtained from a file (thus a String variable).
The type of myVar needs to match that type.

How can I rewrite the 2nd line to enable it to get the type from nameString?

Thank you so much for your help.

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

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

发布评论

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

评论(2

七婞 2024-10-18 00:28:52

这没有多大意义。如果 myVar 的类型不是静态已知的,您将只能通过反射使用它。在这种情况下,只需将其存储为对象即可。

您是否正在尝试验证 classMethod() 是否返回预期类型?或者您是否尝试将结果转换为特定类型,以便可以对其调用特定方法?换句话说,接下来你打算用 myVar 做什么?调用类似 myVar.myMethod() 的方法?


下面是使用反射调用对象方法的示例。参考 文档以获得更详细的解释。

Object myVar = AnotherClass.classMethod();
/* Get the class object for "myVar" to access its members. */
Class<?> myClass = myVar.getClass();
/* Find the public, no-arg method "myMethod()". */
Method mth = myClass.getMethod("myMethod");
/* Invoke "myMethod()" on "myVar", and assign result to "r". */
Object r = mth.invoke(myVar);

This doesn't make a lot of sense. If the type of myVar is not statically known, you will only be able to use it via reflection. In that case, just store it as an Object.

Are you trying to verify that classMethod() is returning the expected type? Or are you trying to cast the result to a specific type so that you can invoke specific methods on it? In other words, what are you going to do with myVar next? Invoke a method like myVar.myMethod()?


Here's an example of invoking a method on an object using reflection. Refer to the Class documentation for a more detailed explanation.

Object myVar = AnotherClass.classMethod();
/* Get the class object for "myVar" to access its members. */
Class<?> myClass = myVar.getClass();
/* Find the public, no-arg method "myMethod()". */
Method mth = myClass.getMethod("myMethod");
/* Invoke "myMethod()" on "myVar", and assign result to "r". */
Object r = mth.invoke(myVar);
久夏青 2024-10-18 00:28:52

Class 的实例:

Class<?> myclass = Class.forName("myClass_t");
myClass_t myVar = (myClass_t)myclass.cast(AnotherClass.classMethod());

使用 请注意,这可能会引发多个异常,例如,如果字符串定义的类不存在,或者 AnotherClass.classMethod() 未返回要转换为的类的实例。
澄清接口的想法(通常用于 Java 中的插件机制):

interface Testing {
    public String getName();
}

class Foo implements Testing
{
    public String getName()
    {
        return "I am Foo";
    }
}

class Bar implements Testing
{
    public String getName()
    {
        return "I am Bar";
    }
}

// Then
Class<?> myclass = Class.forName("Foo");
Testing instance = (Testing)myclass.newInstance();
System.out.println(instance.getName()); // I am a Foo
myclass = Class.forName("Bar");
Testing instance = (Testing)myclass.newInstance();
System.out.println(instance.getName()); // I am a Bar

基本上你有一个动态类名(例如来自属性文件)。此类实现了一个接口,因此您可以确保类实例提供接口方法(而不是对所有内容都使用反射)。

Use an instance of Class:

Class<?> myclass = Class.forName("myClass_t");
myClass_t myVar = (myClass_t)myclass.cast(AnotherClass.classMethod());

Be aware that this might throw several Exceptions, e.g. if the class defined by the string does not exist or if AnotherClass.classMethod() doesn't return an instance of the class you want to cast to.
To clarify the interface idea (which is usally used for plugin mechanisms in Java):

interface Testing {
    public String getName();
}

class Foo implements Testing
{
    public String getName()
    {
        return "I am Foo";
    }
}

class Bar implements Testing
{
    public String getName()
    {
        return "I am Bar";
    }
}

// Then
Class<?> myclass = Class.forName("Foo");
Testing instance = (Testing)myclass.newInstance();
System.out.println(instance.getName()); // I am a Foo
myclass = Class.forName("Bar");
Testing instance = (Testing)myclass.newInstance();
System.out.println(instance.getName()); // I am a Bar

Basically you have a dynamic class name (e.g. from a properties file). This class implements an interface, so you can make sure that class instances provide the interfaces methods (instead of using reflection for everything).

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