32 lines
767 B
Java
32 lines
767 B
Java
// polymorphism/Shapes.java
|
|
// ©2016 MindView LLC: see Copyright.txt
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
|
// Polymorphism in Java.
|
|
import polymorphism.shape.*;
|
|
|
|
public class Shapes {
|
|
private static RandomShapeGenerator gen =
|
|
new RandomShapeGenerator();
|
|
public static void main(String[] args) {
|
|
Shape[] s = new Shape[9];
|
|
// Fill up the array with shapes:
|
|
for(int i = 0; i < s.length; i++)
|
|
s[i] = gen.get();
|
|
// Make polymorphic method calls:
|
|
for(Shape shp : s)
|
|
shp.draw();
|
|
}
|
|
}
|
|
/* Output:
|
|
Triangle.draw()
|
|
Triangle.draw()
|
|
Square.draw()
|
|
Triangle.draw()
|
|
Square.draw()
|
|
Triangle.draw()
|
|
Square.draw()
|
|
Triangle.draw()
|
|
Circle.draw()
|
|
*/
|