JAVA上机实验求圆的面积和周长 第3页
public Rectangle(double value) {
this(value, value);
}
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public String toString() {
return "My real Class is:" + Rectangle.class.getName()
+ "\nMy length is:" + length + "\nMy width is:" + width
+ "\nMy area is:" + area() + "\nMy perimeter is:" + perimeter();
}
@Override
public double area() {
return length * width;
}
@Override
public double perimeter() {
return 2 * (length + width);
}
@Override
public void draw() {
Logger logger = Logger.getLogger("INFO");
logger.info("draw()");
System.out.println(message());
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
}
package fox.math.kmust;
/**
* @file Test.java
*/
public final class Test {
public static void main(String[] args) {
Shape shape = null;
shape = new Circle();
getShapeData(shape);
if (Graph.class.isInstance(shape)) {
drawGraph((Graph) shape);
}
System.out.println("-----The Line For Fox" + "-----TLFF-----"
+ "The Line For Fox-----");
shape = new Rectangle(3.0);
getShapeData(shape);
if (Graph.class.isInstance(shape)) {
drawGraph((Graph) shape);
}
}
private static void getShapeData(Shape shape) {
System.out.println("area:" + shape.area());
System.out.println("perimeter:" + shape.perimeter());
}
private static void drawGraph(Graph graph) {
graph.draw();
}
}