2
How would it be possible to build a differentiated constructor for classes that extend a generic class?
For example, for the situation below, I would like to create a Generic builder that can perform the instruction Entity = E.class;, but for now I could not. So that it could be invoked in the constructor of the Itemmanager class.
How could we do that?
public class Generic<E> {
private Class<E> entity;
public void setEntity(Class<E> entity) {
this.entity = entity;
}
public Generic() {
entity = E.class;
}
}
class ItemManager extends Generic<Item> {
public ItemManager() {
super();
}
}
entity
should reference an object, it cannot reference a class. Tell us what is the intention of this application to try to find a solution. Actually I didn’t understand that line either:private Class<E> entity;
can do this? What is the result?– Math
Yes, that line is possible. And except the builder public generic, everything works. But, I need in each constructor to make a setEntity, that is, within the constructor of Itemmanager, instead of just calling super, I need to make a setEntity(Entity.class) for example. But this Entity is just the E parameter that I’m passing to the Generic, so I wanted to automate that. The reason for this is to make a generic class to operate on the database with various automated methods.
– Grão-Lorde