package ObjectEX; class Book {}
package ObjectEX; class ComputerBook extends Book { public String toString(){ return "ComputerBook Object"; }}
package ObjectEX; class ObjectEX { public static void main (String[] args){ Book book1 = new Book(); ComputerBook book2 = new ComputerBook(); System.out.println(book1); //when the parameter of println() is object, //it will automatically call the toString() method.[class name + memory address ] System.out.println(book2); //the toString method has been override Object [] obj = new Object[2]; obj[0] = book1; obj[1] = book2; for (Object o: obj) System.out.println(o); }}
运行结果:
ComputerBook ObjectComputerBook Object