不想写文字啊!
1 package typeinfo; 2 3 import java.util.*; 4 5 abstract class Shape{ 6 void draw(){ System.out.println(this + ".draw()");} 7 abstract public String toString(); 8 } 9 10 class Circle extends Shape{ 11 public String toString(){ 12 return "Circle"; 13 } 14 } 15 16 class Square extends Shape{ 17 public String toString(){ 18 return "Square"; 19 } 20 } 21 22 class Triangle extends Shape{ 23 public String toString(){ 24 return "Triangle"; 25 } 26 } 27 28 29 public class Shapes { 30 public static void main(String [] args){ 31 List<Shape> shapelist = Arrays.asList( 32 new Circle(), new Square(),new Triangle()); 33 for(Shape shape:shapelist) 34 shape.draw(); 35 } 36 }
结果:
1 Circle.draw() 2 Square.draw() 3 Triangle.draw()
原文:http://www.cnblogs.com/fxyfirst/p/3812730.html