first commit
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
.settings/
|
||||
bin/
|
||||
.classpath
|
||||
.project
|
||||
*.class
|
||||
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,63 @@
|
||||
package org.palomafp.aadd.dta;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class AppPrincipal {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
String literalClase = "org.palomafp.aadd.dta.model.Continente";
|
||||
|
||||
try {
|
||||
Class<?> claseContinente = Class.forName(literalClase);
|
||||
|
||||
// instrospección: imprimir métodos y atributos
|
||||
|
||||
System.out.println("--- INTROSPECCIÓN: ATRIBUTOS Y MÉTODOS ---");
|
||||
|
||||
System.out.println("Atributos:");
|
||||
|
||||
Field[] atributos = claseContinente.getDeclaredFields();
|
||||
|
||||
for (Field atributo : atributos) {
|
||||
System.out.println(" - " + atributo.getType().getSimpleName() + " " + atributo.getName());
|
||||
}
|
||||
|
||||
System.out.println("Métodos:");
|
||||
|
||||
Method[] metodos = claseContinente.getDeclaredMethods();
|
||||
|
||||
for (Method metodo : metodos) {
|
||||
System.out.println(" - " + metodo.getName() + "()");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
|
||||
// reflexión: construir y cargar el objeto
|
||||
|
||||
System.out.println("--- REFLEXIÓN: CONSTRUCCIÓN Y CARGA ---");
|
||||
|
||||
Object miContinente = claseContinente.getDeclaredConstructor().newInstance();
|
||||
System.out.println("Objeto instanciado correctamente por reflexión.");
|
||||
|
||||
Method metodoSetCodigo = claseContinente.getDeclaredMethod("setCodigo", String.class);
|
||||
metodoSetCodigo.invoke(miContinente, "EU");
|
||||
|
||||
Method metodoSetNombre = claseContinente.getDeclaredMethod("setNombre", String.class);
|
||||
metodoSetNombre.invoke(miContinente, "Europa");
|
||||
|
||||
System.out.println("Valores asignados correctamente al objeto.");
|
||||
|
||||
System.out.println();
|
||||
|
||||
// imprimir el objeto
|
||||
System.out.println("--- OBJETO FINAL ---");
|
||||
System.out.println(miContinente);
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("Ha ocurrido un error durante la ejecución: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.palomafp.aadd.dta.model;
|
||||
|
||||
public class Continente {
|
||||
|
||||
private String codigo;
|
||||
private String nombre;
|
||||
|
||||
public Continente() {}
|
||||
|
||||
public String getCodigo() {
|
||||
return codigo;
|
||||
}
|
||||
|
||||
public void setCodigo(String codigo) {
|
||||
this.codigo = codigo;
|
||||
}
|
||||
|
||||
public String getNombre() {
|
||||
return nombre;
|
||||
}
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Continente [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user