父类GeometricObject:
import java.util.Date;
public class GeometricObject {
private String color="white";
private boolean filled;
private Date dateCreated= new Date();
public GeometricObject(){
}
public GeometricObject(String color,boolean filled){
this.color=color;
this.filled=filled;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public Date getDateCreated() {
return dateCreated;
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public String toString() {
return "GeometricObject [color=" + color + ", filled=" + filled
+ ", dateCreated=" + dateCreated + "]";
}
}
子类1:Circle
public class Circle extends GeometricObject{
private double radius;
public Circle(){
}
//创建一个参数对象时的构造方法
public Circle(double radius){
this.radius = radius;
}
//创造4个参数时的构造方法(本代码中未使用,可以直接不写)
public Circle(double radius,String color,boolean filled){
this.radius=radius;
setColor(color);
setFilled(filled);
}
/** Return radius */
public double getRadius() {
return radius;
}
/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}
/** Return area */
public double getArea() {
return radius * radius * Math.PI;
}
/** Return diameter */
public double getDiameter() {
return 2 * radius;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}
/* Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +" and the radius is " + radius);
}
}
子类2:Rectangle
public class Rectangle extends GeometricObject{
private double width;
private double height;
public Rectangle(){
}
public Rectangle(double width,double height){
this.width=width;
this.height = height;
}
//创造4个参数时的构造方法,本代码未使用可以省略
public Rectangle(double width, double height, String color, boolean filled) {
this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double width) {
this.width = width;
}
/** Return height */
public double getHeight() {
return height;
}
/** Set a new height */
public void setHeight(double height) {
this.height = height;
}
/** Return area */
public double getArea() {
return width * height;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * (width + height);
}
}
测试类:
public class TestCircleRectangle {
public static void main(String[] args) {
Circle circle = new Circle(1);
System.out.println("A circle " + circle.toString());
System.out.println("The color is " + circle.getColor());
System.out.println("The radius is " + circle.getRadius());
System.out.println("The area is " + circle.getArea());
System.out.println("The diameter is " + circle.getDiameter());
Rectangle rectangle = new Rectangle(2, 4);
System.out.println("\nA rectangle " + rectangle.toString());
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " + rectangle.getPerimeter());
}
}