java - 反射是否反映Java中的继承?
问题描述
在 Java 中,我有一个映射,其中键是 Class<BaseType> 对象。每个条目都以 Class<ExtendedType> 的对象为键,其中 ExtendedType 扩展了 BaseType。(有许多不同的 ExtendedType 类,比如 ExtendedType1、ExtendedType2 等,只有一个 BaseType。)
Map<Class<BaseType>, String> myMap;
我知道它适用于我们的项目。但是,我想知道这在语法上是否正确?
如果语法没问题,那么以下内容也应该没问题(除了两行 - 请参阅代码中的注释):
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?
}
你怎么看?感谢您的任何提示/评论。
解决方案
推荐阅读
- batch-file - 批量删除确切的文件扩展名
- php - laravel 的 rest api 中的事件处理
- mysql - 无法让我的 SUM() 在 3 个组条件下正确显示
- python - 如何在 Python 中解密 SELECT * 语句的一列?
- reactjs - React Native Typescript Formik 强制转换事件类型
- java - JSP 文件未检测到用户定义的 java 文件。请解决
- java - Eclipse中这个图标是什么意思?
- r - 如何识别 R 中列中的模式并为所述模式赋予标识
- javascript - 如何递归替换对象中的键名?
- angular - Ionic 4 防止缩短产品构建的类名