Java:在hashmap中使用类作为值 [英] Java: Using Classes as a value in hashmap

查看:138
本文介绍了Java:在hashmap中使用类作为值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 Map< String,Class<?> 中使用自定义类作为值。以下是代码的相关部分:

I'm trying to use custom Class as a value in a Map<String, Class<?>>. Following are relevant parts of code:

以下是在 main()中声明和初始化Map

public static Map<String, Class<?>> mapQuery2ResponseType = new HashMap<String, Class<?>>();
static {
    mapQuery2ResponseType.put("string1", CustomClass1.class);
    mapQuery2ResponseType.put("string2", CustomClass2.class);
    mapQuery2ResponseType.put("string3", CustomClass3.class);
}



现在我使用这个地图来将一个对象:(假设所有类都包含一个方法 getName(),它返回一个 String

String name = (
                  (
                       mapQuery2ResponseType.get("string1")
                  )obj1
              ).getName();

其中, obj1 code> T ,

where, obj1 is of generic type T,

但不允许我这样做,并说: obj1,删除此标记

but it's not allowing me to do so and says: Syntax error on token "obj1", delete this token.

请帮我明白我在哪里做错了吗?

Please help me to understand where am I doing wrong?

编辑:

当我使用以下代码时, ,

When I use following code, it worked perfectly giving me the expected result,

String name = (
                  (
                       CustomClass1
                  )obj1
              ).getName();

obj1 mapQuery2ResponseType.put(string1,CustomClass1.class);

这里我可以看到1个东西...如果我直接使用它,我使用它作为CustomClass1,而如果我从地图得到它 mapQuery2ResponseType.get(string1),它返回CustomClass1.class。我不知道这两种方法有什么区别吗?如果有,它是什么?

Here I can see 1 thing... if I use use it directly, i use it as "CustomClass1", whereas if I get it from map by mapQuery2ResponseType.get("string1"), it returns "CustomClass1.class". I'm not sure if there is any difference in these two approaches? If there is, what is it?

所以实际上没有任何转换,只是我使用它大量的类

So actually there wont be any conversion, it's just that I'm using it for large number of classes, and so trying to use a generalized approach.

Edit2:

如下所示: Java:CustomClass1和CustomClass1.class之间的区别?我认为,反射是这个任务的唯一解决方案。

as given in this question: Java: difference between "CustomClass1" and "CustomClass1.class"?, I think, reflection is the only solution for this task. But can anybody explain how to do it using reflection?

推荐答案

简单地说,这是语法无效的代码。

Simply put, this is syntactically invalid code. Not coincidentally, the compiler is telling you exactly what the problem is.

删除

原代码:

String name = ((mapQuery2ResponseType.get("string1"))obj1).getName();

现在有了一个局部变量:

Now with a local variable:

Class<?> clazz = mapQuery2ResponseType.get("string1");
String name = ((clazz) obj1).getName();

好吧,现在我想我看到了:你试图使用 Class 实例来转换对象。您无法执行此操作 - Java语法这里是一个演员通常看起来的样子:

Okay, so now I think I see: you're trying to use a Class instance to cast an object. You can't do this – Java's syntax simply does not permit it. Here's how a cast might normally look:

Object foo = "bar";
String baz = (String) foo;

注意转换表达式的令牌是 String ,而不是 String.class

Note how the token for the cast expression is String, not String.class.

Class#cast() 可能看起来像一个有用的替代品,但这不会在这里使用,因为你只有类<?> ,而不是类< T>

忘记 Map 。我建议您完全重构您的代码,使 getName()方法定义在 obj1 和所有类似的对象实现(假设各种对象都有异类)。

Forget about the Map. I suggest you restructure your code entirely such that the getName() method is defined on an interface which obj1 and all similar objects implement (assuming that the various objects have heterogenous types).

例如:

interface MyCommonInterface {
    String getName();
}

class MyClass implements MyCommonInterface {
    public String getName() {
        // snip
    }
}

// ...
MyClass obj1 = /* ... */
String name = obj1.getName();

没有投射,没有幻想地图 实例。只要正确,简单地使用语言的正确部分。

No casting, no fancy Maps or Class instances. Just proper, simple use of the right parts of the language.

这篇关于Java:在hashmap中使用类作为值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆