English 中文(简体)
基本java等于(......)高于需要的救助
原标题:Basic java equals() override help needed

我目前正在着手在java实施一个简单的线方案。 不存在真正的全球倡议,因此它完全是建立在案文基础上的。

要求我们有一个点级和一个线级,包括点击物体。

我遇到的问题涉及我的级别上平等的方法。 考虑到每一点只具有两种固定价值,即x和a y,而我在此有问题,我担心,如果我不得不比较界线,我将有问题,因为这将涉及对各点、暗中宽和有色的肤色进行比较。

这就是我对我的同级方法的守则。

@Override
public boolean equals(Point that) {
    if(this==that)
        return true;
    //if
    if(this.x==that.getX() && this.y==that.getY())
        return true;
    return false;
}

感谢一切帮助。

最佳回答

签字必须含有目的,而不是点。 那么,你们需要明确的检查,以确保目标事实上是个点,它不会造成损失。

此外,正如你说,我看不出这种方法有任何问题,就象我能够说的那样,这种方法具有灵活性、不对称性、一贯性和过境性。 如果您的班子使用双倍,那么我就说,在比较时,会给他们带来太大的价值,但显然与不成问题的人相比。

间接问题是,你实际上应当超越散列码,并且以同样的方式行事,否则,你在把点添加到使用<条码>hashcode(<><>>>/code(>)的收集点时,会遇到一些奇怪的问题(根据他们希望以同样方式比较物体的合同)。

问题回答

对于仅包含两个分类的简单类别,应当采用以下分类法和平等法:

/*
 * (non-Javadoc)
 * 
 * @see java.lang.Object#hashCode()
 */
@Override
public int hashCode()
{
    final int prime = 31;
    int result = 1;
    result = prime * result + x;
    result = prime * result + y;
    return result;
}

/*
 * (non-Javadoc)
 * 
 * @see java.lang.Object#equals(java.lang.Object)
 */
@Override
public boolean equals(Object obj)
{
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Point other = (Point) obj;
    if (x != other.x)
        return false;
    if (y != other.y)
        return false;
    return true;
}

你显示,“线”级方法相同,但我认为“线”可能等于点。 线路可以包含一个点。 您的意思是点上平等的方法?

你们的问题似乎是业绩和目的。 页: 1

您的班子应当优先于“hCode()”方法。

Look at the definition of the Object.equals() method. In particular look at the type of the parameter. Any override of equals() must have an Object parameter.

public boolean equals(Object that) { ... }

让我尝试:

@Override
public boolean equals(Object o) 
{
    //Cast the object o to Point instance
    if(!(o instanceof Point))
        return false;
    Point obj = (Point)o;
    if(obj==null)
        return false;//Checking if null
    else if.....//test properties using cast object obj
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签