JvWill
Setembro 22, 2004, 4:26pm
#1
Olá amigos,
é possível pegar o nome da classe que está instanciada em um atributo ?
Por exemplo:
Teste test = new Teste();
existe algum método que me retorno como String o nome da classe que está instanciada na variável test ?
Agradeço a todos de antemão,
Wil
test.getClass().getName()
nao sei se eh isso mesmo…
getClass
public final Class getClass()
Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.
Returns:
the object of type Class that represents the runtime class of the object.
Referencia:
http://java.sun.com/j2se/1.4.2/docs/api/index.html
thingol
Setembro 22, 2004, 4:50pm
#4
class Pessoa {
String nome;
}
class Aluno extends Pessoa {
}
class Professor extends Pessoa {
}
class Test123 {
public static void main(String[] args) {
Pessoa p1 = new Aluno();
Pessoa p2 = new Professor();
System.out.println (p1.getClass().getName());
System.out.println (p2.getClass().getName());
}
}
Rode o programa Test123, a saída deve ser:
C>java -cp . Test123
Aluno
Professor
JvWill
Setembro 22, 2004, 5:17pm
#5
Obrigado a todos, é isto mesmo que eu queria !