Σχήματα: ο κώδικας
abstract class Shape {
private double x, y; // Position
protected double getX() { return x; }
protected double getY() { return y; }
public void setposition(double px, double py) {
x = px;
y = py;
}
public abstract double area();
@Override public String toString() {
return "Shape(x=" + x + ", y=" + y + ", area=" + area() + ")";
}
}
class Circle extends Shape {
private double radius;
public void setradius(double r) {
radius = r;
}
@Override public double area() {
return Math.PI * radius * radius;
}
@Override public String toString() {
return super.toString() + ": Circle(" + radius + ")";
}
}
class Rectangle extends Shape {
private double height, width;
public void setdimensions(double h, double w) {
height = h;
width = w;
}
@Override public double area() {
return height * width;
}
@Override public String toString() {
return super.toString() + ": Rectangle(" + height + " x " + width + ")";
}
}
class Test2 {
static public void main(String args[])
{
Circle c = new Circle();
Rectangle r = new Rectangle();
Shape s[] = new Shape[2];
s[0] = r;
r.setposition(1, 2);
r.setdimensions(50, 50);
s[1] = c;
c.setposition(3, 4);
c.setradius(10);
for (int i = 0; i < s.length; i++)
System.out.println(s[i]);
}
}
Το παραπάνω πρόγραμμα θα τυπώσει:
Shape(x=1.0, y=2.0, area=2500.0): Rectangle(50.0 x 50.0)
Shape(x=3.0, y=4.0, area=628.3185307179587): Circle(10.0)