In Java, I have a map where keys are Class<BaseType> objects. Each entry is keyed with an object of Class<ExtendedType> where ExtendedType extends BaseType. (There are many different ExtendedType classes, say ExtendedType1, ExtendedType2, etc., and just one BaseType.)
Map<Class<BaseType>, String> myMap;
I know it works on our project. However, I wonder whether this is syntactically correct?
If the syntax is fine, then the following should be fine as well (except for two lines - see comments in code):
class A {
}
class B extends A {
}
public static void main(String[] args) {
// First inheritance:
A a = new A();
// Do something with a.
B b = new B();
// Do something with b.
a = new B();
// Should be fine since B extends A.
b = new A();
// Should NOT compile. A is a superclass of B.
// Now reflection:
Class<A> classA = A.class;
// Do something with classA.
Class<B> classB = B.class;
// Do something with classB.
classA = B.class;
// Should be fine since Class<B> extends Class<A>. Does it really?
classB = A.class;
// Should NOT compile. Class<A> is a superclass of Class<B>. Is it?
}
What do you think? Thanks for any hints/comments.
Aucun commentaire:
Enregistrer un commentaire