For example, I have classes:
例如,我有课程:
SuperClass.java:
public class SuperClass<A extends ClientsDTO> {
...
private A getChildsGenericClass{
//???
}
}
User.java
class User extends SuperClass<UserDTO> {
...
}
So how can I access UserDTO's class in SuperClass
's in getChildsGenericClass
method?
那么如何在getChildsGenericClass方法中访问SuperClass中的UserDTO类呢?
2 个解决方案
#1
0
public A example; //this is variable from you can get Class
private Class<Object> getChildsGenericClass() {
return (Class)((ParameterizedType)
(this.getClass().getGenericSuperclass()))
.getActualTypeArguments()[0];
}
that actually works!
这实际上有效!
#2
0
you did not pass an instance of UserDTO to the superclass.
without instance, which objects fields do you want to access?
您没有将UserDTO的实例传递给超类。没有实例,您想要访问哪些对象字段?
When you want to access UserDTO class, you can easily write:
当您想要访问UserDTO类时,您可以轻松编写:
Class c = UserDTO.class;
in other case, maybe you want something like this:
在其他情况下,也许你想要这样的东西:
public class SuperClass<T extends ClientsDTO> {
private Class<T> m_clz;
public NewClass(Class<T> p_clz)
{
m_clz = p_clz;
}
private Class<T> getChildsGenericClass()
{
return m_clz;
}
}
#1
0
public A example; //this is variable from you can get Class
private Class<Object> getChildsGenericClass() {
return (Class)((ParameterizedType)
(this.getClass().getGenericSuperclass()))
.getActualTypeArguments()[0];
}
that actually works!
这实际上有效!
#2
0
you did not pass an instance of UserDTO to the superclass.
without instance, which objects fields do you want to access?
您没有将UserDTO的实例传递给超类。没有实例,您想要访问哪些对象字段?
When you want to access UserDTO class, you can easily write:
当您想要访问UserDTO类时,您可以轻松编写:
Class c = UserDTO.class;
in other case, maybe you want something like this:
在其他情况下,也许你想要这样的东西:
public class SuperClass<T extends ClientsDTO> {
private Class<T> m_clz;
public NewClass(Class<T> p_clz)
{
m_clz = p_clz;
}
private Class<T> getChildsGenericClass()
{
return m_clz;
}
}