ソースコード (9-4)
class Rectangle {
double x;
double y;
double width;
double height;
public Rectangle(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void printout() {
System.out.println(this.x);
System.out.println(this.y);
System.out.println(this.width);
System.out.println(this.height);
}
}
class Square extends Rectangle {
public Square(double x, double y, double size) {
super(x, y, size, size);
}
public void printout() {
System.out.println(this.x);
System.out.println(this.y);
System.out.println(this.width);
}
}
class ColorRectangle extends Rectangle {
String color;
public ColorRectangle(double x, double y, double width, double height, String color) {
super(x, y, width, height);
this.color = color;
}
public void printout() {
System.out.println(this.x);
System.out.println(this.y);
System.out.println(this.width);
System.out.println(this.height);
System.out.println(this.color);
}
}
public class YourClassNameHere {
public static void main(String[] args) {
Rectangle a = new Rectangle(4, 8, 1, 2);
Rectangle b = new Rectangle(8, 10, 2, 1);
a.printout();
b.printout();
Square c = new Square(0, 3, 1);
Square d = new Square(1, 1, 2);
c.printout();
d.printout();
ColorRectangle e = new ColorRectangle(0, 0, 1, 4, "Red");
e.printout();
}
}
45