If I have not misunderstood, you should ask the objects a question, your code would look like this:
class Animal {}
class Perro extends Animal {
public static void main (String[] args){
Perro toby = new Perro();
if (toby instanceof Animal)
System.out.println("toby es un perro y también un animal");
}
}
In this way with the instanceof
you know if they extend from one class or another, where I have Animal
you will put the class that interests you as MakeAPlayer
.
I hope I have helped you, but you already know, tell me about it.
I continue with your comment:
So what you need is a creation of a generic class where this will be a subtype of your class MakeAPlayer
.
An example code could be like this:
Person p = new Person();
Class<? extends Person> clase = p.getClass();
try {
Person p = clase.newInstance();
} catch (InstantiationException e) {
System.out.println(p.getName() + " no puede ser instanciada");
System.out.println("Puede ser que " + p.getName() + " sea una clase abstracta," +
" una interfaz, un vector (array), a primitive type, or void;");
e.printStackTrace();
} catch (IllegalAccessException e) {
System.out.println("La clase " + p.getName() + "no tiene un constructor " +
"vacío y público");
e.printStackTrace();
} // try
I leave you a source of information where you explain in a synthesized way what you need.
link
answered by
20.08.2016 в 14:07