我知道 java Constructor 转换成 Scala 代码。但这是我的项目通用类实现。我在这个项目中使用hibernate,spring并且我创建了genericDAO trait但无法创建它的实现或者我无法将此java构造函数转换为scala
这是 2 个变量
private Class<T> entityClass;
private String entityClassName;
这是这里的 GenericDAOImpl 类代码 GenericDAOImpl 是构造函数
@SuppressWarnings("unchecked")
public GenericDAOImpl() {
Type genericSuperclass;
Class<?> parametrizedClass = getClass();
do {
genericSuperclass = parametrizedClass.getGenericSuperclass();
if (genericSuperclass instanceof Class) {
parametrizedClass = (Class<?>) genericSuperclass;
}
} while (genericSuperclass != null
&& !(genericSuperclass instanceof ParameterizedType));
this.entityClass = (Class<T>) ((ParameterizedType) genericSuperclass)
.getActualTypeArguments()[0];
if (entityClass != null) {
entityClassName = entityClass.getSimpleName();
}
}
来自米兰的感谢
编辑
我试过这个
@SuppressWarnings("unchecked")
def this(T,ID){
var genericSuperclass:Type;
var parametrizedClass:Class[?]=getClass
do {
genericSuperclass = parametrizedClass.getGenericSuperclass()
if (genericSuperclass instanceof[Class]) {
parametrizedClass = (Class<?>) genericSuperclass
}
} while (genericSuperclass != null
&& !(genericSuperclass instanceof [ParameterizedType]))
this.entityClass = (Class[T]) ((ParameterizedType) genericSuperclass)
.getActualTypeArguments()[0]
if (entityClass != null) {
entityClassName = entityClass.getSimpleName()
}
}
And this got compilation Error