简体   繁体   English

getClass返回什么类型?

[英]What type does getClass return?

Here is my code: 这是我的代码:

Set<Class<Event>> s = new HashSet<>();             
Set<Class<? extends Event>> s2 = new HashSet<>();
Event e = new Event();                         

s.add(e.getClass());   // #1
s2.add(e.getClass());  // #2

class Event {
    // ...
}

Why does the compiler raise an error on the statement #1 ? 为什么编译器会在语句#1上引发错误?

I'm using Java 7. 我正在使用Java 7。

If you take a look at documentation of getClass() method you will see that 如果你看一下getClass()方法的文档,你会看到它

The actual result type is Class<? extends |X|> 实际的结果类型是Class<? extends |X|> Class<? extends |X|> where |X| Class<? extends |X|> where | X | is the erasure of the static type of the expression on which getClass is called. 是调用getClass的表达式的静态类型的擦除。 For example, no cast is required in this code fragment: 例如,此代码片段中不需要强制转换:

 Number n = 0; Class<? extends Number> c = n.getClass(); 

So result of e.getClass() will be Class<? extends Event> 因此e.getClass()结果将是Class<? extends Event> Class<? extends Event> which is precisely what Set<Class<? extends Event>> s2 Class<? extends Event>这正是Set<Class<? extends Event>> s2 Set<Class<? extends Event>> s2 is suppose to store. Set<Class<? extends Event>> s2假设存储。 That is why 这就是为什么

s2.add(e.getClass());   // OK 

works fine. 工作正常。


But in case of Set<Class<Event>> s things are little different. 但是在Set<Class<Event>> s情况下,事情会有所不同。 It can store only Class<Event> . 它只能存储Class<Event> Allowing it to store objects from Class<? extends Event> 允许它存储Class<? extends Event>对象 Class<? extends Event> reference would be very dangerous in terms of type-security. Class<? extends Event>引用在类型安全性方面会非常危险。

Take a look at this example (for easier understanding lets replace Class with List and our Action instances will be Animal s like Dog and Cat ). 看看这个例子(为了便于理解,可以代替ClassList和我们的Action实例将Animal就像DogCat )。

List<Dog> dogs = new ArrayList<>(); 
List<? extends Animal> generalList = dogs;

Set<List<Animal>> set = new HashSet<>();

Now lets assume that Set<List<Animal>> set can store List<? extends Animal> 现在假设Set<List<Animal>> set可以存储List<? extends Animal> List<? extends Animal>

set.add(generalList);

Now we are able to do something horrible as 现在我们可以做一些可怕的事了

for (List<Animal> animalList : set){
    animalList.add(new Cat()); // I just placed Cat in container full of Dogs!
}

Remember our List<Dog> dogs = new ArrayList<>(); 记住我们的List<Dog> dogs = new ArrayList<>(); list? 清单? Now it contains Cat so if I do: 现在它包含Cat ,如果我这样做:

for (Dog dog : dogs){
    dog.speak();
}

I will probably see something similar to 我可能会看到类似的东西

wof WOF
woof
Woof
Meow! 喵! (psst: Get me out of here!) (psst:让我离开这里!)
... ...

or instead of Meow! 或者代替Meow! some exception like NoSuchMethodException or most probably ClassCastException: Cat cannot be cast to Dog . NoSuchMethodException或者很可能是ClassCastException: Cat cannot be cast to Dog

So as you see allowing this mechanism wouldn't be very wise. 所以当你看到允许这种机制不是很明智。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM