public class Caja { private Object dato; public Caja() { super(); } public Object dame() { return dato; } public void pon(Object x) { dato = x; } }
public static void main(String args[]) { Caja c = new Caja(); c.pon(“Hola”); Integer n = (Integer) c.dame(); }
public class Caja <T> { private T dato; public Caja() { super(); } public T dame() { return dato; } public void pon(T x) { dato = x; } }
Caja <Integer> c;
public class TestCaja { public static void main(String args[]) { Caja <Integer> c = new Caja <Integer> (); c.pon(new Integer(46)); Integer n = c.dame(); } }
Caja <Integer> c;
public static void main(String args[]) { Caja <String> c = new Caja <String> (); c.pon(“Hola”); Integer n = c.dame(); }
public static <T> void mostrarArray(ArrayList<T> a) { for (int i = 0; i < a.size(); i++) System.out.println(a.get(i).toString()); }
public interface Comparable<T> { public int compareTo(T x); // Devuelve < 0 , si this < x // Devuelve > 0 , si this > x // Devuelve == 0 , si this == x }
public abstract class Figura implements Comparable<Figura> { … public int compareTo(Figura f) { if (this.area() < f.area()) return -1; else if (this.area() > f.area()) return 1; else return 0; } …