1. 程式人生 > >java 語言程式設計 第十三章 13.5

java 語言程式設計 第十三章 13.5

程式小白,希望和大家多交流,共同學習
這裡寫圖片描述
(只是將13.1的內容加以修改,具體的就是 implements Comparable;
然後我又在Rectangle類中重寫了toString()方法,下面還給了一個關於Rectangle類的測試類。)
這裡寫圖片描述

public abstract class CompareGeometricObject 
    implements Comparable<CompareGeometricObject>
{
    private String color;
    private boolean filled;
    private
java.util.Date dateCreated; protected CompareGeometricObject() { dateCreated = new java.util.Date(); } protected CompareGeometricObject(String color, boolean filled) { this.color = color; this.filled = filled; dateCreated = new java.util.Date(); } public
String getColor() { return color; } public void setColor(String color) { this.color = color; } public boolean isFilled() { return filled; } public void setFilled(boolean filled) { this.filled = filled; } public java.util.Date getDateCreated
() { return dateCreated; } @Override public String toString() { return "Created on " + dateCreated + "\nColor : " + color + "\n and Filled : " + filled; } @Override public int compareTo(CompareGeometricObject o) { if (getArea() < o.getArea()) { return -1; } else if (getArea() > o.getArea()) { return 1; } else return 0; } public static CompareGeometricObject max(CompareGeometricObject o1, CompareGeometricObject o2) { if (o1.compareTo(o2) > 0) { return o1; } else return o2; } public abstract double getArea(); public abstract double getPerimeter(); }
public class CompareMyRectangle extends CompareGeometricObject
{
    private double width;
    private double height;

    public CompareMyRectangle()
    {
    }

    public CompareMyRectangle(double width, double height)
    {
        this(width, height, "white", false);
    }

    public CompareMyRectangle(double width, double height, String color, boolean filled)
    {
        super(color, filled);
        this.width = width;
        this.height = height;
    }

    public double getWidth()
    {
        return width;
    }

    public void setWidth(double width)
    {
        this.width = width;
    }

    public double getHeight()
    {
        return height;
    }

    public void setHeight(double height)
    {
        this.height = height;
    }

    @Override
    public double getArea()
    {
        return width * height;
    }

    @Override
    public double getPerimeter()
    {
        return 2 * (width + height);
    }

    @Override
    public String toString()
    {
        return super.toString() + "\nMy Rectangle Width : " + getWidth() + " Height : " + getHeight();
    }
}
import java.util.Scanner;

public class TestCompareRectangle
{
    public static void main(String [] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Create the first Rectangle (enter width, height): ");
        double firstWidth = input.nextDouble();
        double firstHeight = input.nextDouble();

        System.out.print("Crate the second Rectangle (enter width, height): ");
        double secondWidth = input.nextDouble();
        double secondHeight = input.nextDouble();

        CompareMyRectangle rectangle1 = new CompareMyRectangle(firstWidth, firstHeight);
        CompareMyRectangle rectangle2 = new CompareMyRectangle(secondWidth, secondHeight);

        System.out.println(CompareGeometricObject.max(rectangle1, rectangle2).toString());
        // 可以看出此處的 max 返回的是 CompareMyRectangle 的一個例項
    }
}