java - 矩形面向对象开发——Equals()方法

标签 java oop class equals

这里需要一些帮助。正在研究“类和面向对象的开发”,并且可以在我的逻辑和代码方面为课本中的一个问题提供一些帮助。

问题:我被要求修改我之前的 Rectangle 类示例以覆盖 equals() 和 toString() 方法。当两个矩形的长度和宽度都相同时,这两个矩形就相等。

我的方法:我试着改变它来做到这一点,然后决定按面积比较会更容易,而不是同时按宽度和长度进行比较,下面是我所拥有的远的。如果您有任何想法可以帮助,请告诉我。前面有一个 equals() 方法的例子,它比较圆的半径,但在比较两个不同的东西时没有帮助。预先感谢所有!如果您想知道为什么它们不是它们自己的单独文件,我还没有在本章中讲到那里,所以我知道很多东西要看;P

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chapter8javaExamples;

/**
 *
 * @author Eric
 */
public class Rectangle {
    private double length, width;


/**
 * constructor
 * pre: none
 * post: A rectangle class is created. 
 */
public Rectangle() {
    length = 2;   //default length
    width = 4;    //default width
}


/**
 * constructor
 * pre: none
 * post: A rectangle object is created with length and width.
 */
public Rectangle (double l, double w) {
    length = l;
    width = w;

}


/**
 * Changes the length of the rectangle
 * pre: none
 * post: Length has been changed.
 */
public void setLength (double newLength) {
    length = newLength;
}




/**
 * Changes the width of the rectangle.
 * pre: none
 * post: Width has been changed.
 */
public void setWidth (double newWidth) {
    width = newWidth;
}


/**
 * Returns the length of the rectangle.
 * pre: none
 * post: The length of the rectangle has been returned. 
 */
public double getLength() {
    return(length);
}


/**
 * Returns the width of the rectangle.
 * pre: none
 * post: The width of the rectangle has been returned.
 */
public double getWidth() {
    return(width);
}


/**
 * Returns the area of rectangle
 * pre: none
 * post: The area of the rectangle is returned
 */
public double area() {
    double triArea;

    triArea = length * width;
    return(triArea);
}


/**
 * Returns the perimeter of the rectangle
 * pre: none
 * post: The perimeter of the rectangle is returned
 */
public double perimeter() {
    double triPer;

    triPer = length + width + length + width;
    return(triPer);
}


/**
 * Displays the formula for area of a rectangle.
 * pre: none
 * post: The formula is displayed.
 */
public static void displayAreaFormula(){
    System.out.println("The formula for the area of a rectangle is a=l*w");
}

/**
 * Determines if the object is equal to another
 * Circle object.
 * pre: c is a Circle object.
 * post: true has been returned if the objects have
 * the same radii, false otherwise.
 */
public boolean equals(Object r) {
    Rectangle testObj = (Rectangle) r;
    Rectangle testObj2 = (Rectangle) r2;

    if (testObj.getArea() == area && testObj2.getArea == area()) {
        return(true);
    } else {
        return(false);
    }
}


/**
 * Returns a String that represents the Circle object.
 * pre: none
 * post: A string representing the Circle object has
 * been returned.
 */
public String toString(){
    String rectangleString;

    rectangleString = "Rectangle has the Area " + length*width;
    return(rectangleString);
}

/**
 * 
 * @param args 
 */
    public static void main(String [] args){
    Rectangle spot = new Rectangle();
    Rectangle spot2 = new Rectangle(5, 9);

    System.out.println("Area is: " + spot.area());
    System.out.println("Perimeter: " + spot.perimeter());
    Rectangle.displayAreaFormula();
}

最佳答案

我认为在 equals 方法中比较面积不是一个好主意,因为 2x8 的矩形等于 4x4 的矩形,因为两个面积都是 16。这自相矛盾您的要求:

Two rectangles are equal when they both have the same length and width.

在这里,您的 r2 变量未定义。但除此之外,equals 方法不应该比较其他两个对象,它应该将 this 对象与另一个对象进行比较。

如果 r 对象是一个 Rectangle 并且这个矩形的长度与另一个矩形的长度匹配并且这个矩形的宽度与其他矩形的宽度。

关于java - 矩形面向对象开发——Equals()方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16046604/

相关文章:

php - PHP 中的 Bresenham 直线算法

javascript - JS中类方法与className.prototype方法的区别

java - 定义一个与其所在类同名的枚举

java - 在这种并发情况下如何保证数据的一致性?

java - 搜索每条记录具有多个值的类数组

java - 扫描仪从标准 IO 读取的数字错误。 java

java - 为什么 Java 方法需要返回类型

c# - 比较类的两个实例

java - EHCache 消耗的内存与 HashMap 相同

java - 仅基于公共(public)属性动态比较两个不同类的对象