比较对象

发布于 2024-11-14 10:56:25 字数 529 浏览 0 评论 0原文

我有一个表单,允许用户输入有关地址信息的值。 我想将从用户那里获得的值与存储在数据库中的 ADDRESS 表中的信息进行比较。 我有一个实体类

public class Address {
  private String gevernate;
  private int homeNo;
  private String neighborhood;
  private String street;
} 

,它表示为数据库中名为 ADDRESS 的表。

我有一个此类的视图对象,它从 db tabel 返回所有地址值

public static Address getAddress(Connection Con, long stdID) {
// select stamatment and result set object.
}

我面临的问题是表单可能包含Address对象的所有值,它可能只包含2个或3个值,它是在运行时指定的。如何比较两个对象?

I have a form that let user enter values about address information.
I want to compare the values I get from user, with information stored into ADDRESS table in the database.
I have an entity class

public class Address {
  private String gevernate;
  private int homeNo;
  private String neighborhood;
  private String street;
} 

which is represented as a table in the database called ADDRESS.

and I have a view object for this class which return all address values from db tabel

public static Address getAddress(Connection Con, long stdID) {
// select stamatment and result set object.
}

The problem I face is that the form may NOT contain all values of Address object, it may contains only 2 or three values, it's specified at run time. How can I compare two objects ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我也只是我 2024-11-21 10:56:25

向类中添加 equals 方法,如下所示:

public class Address {
    private String gevernate;
    private int homeNo;
    private String neighborhood;
    private String street;

public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Address other = (Address) obj;
    if (gevernate == null) {
        if (other.gevernate != null)
            return false;
    } else if (!gevernate.equals(other.gevernate))
        return false;
    if (homeNo != other.homeNo)
        return false;
    if (neighborhood == null) {
        if (other.neighborhood != null)
            return false;
    } else if (!neighborhood.equals(other.neighborhood))
        return false;
    if (street == null) {
        if (other.street != null)
            return false;
    } else if (!street.equals(other.street))
        return false;
    return true;
}
}

add an equals method to the class like this:

public class Address {
    private String gevernate;
    private int homeNo;
    private String neighborhood;
    private String street;

public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Address other = (Address) obj;
    if (gevernate == null) {
        if (other.gevernate != null)
            return false;
    } else if (!gevernate.equals(other.gevernate))
        return false;
    if (homeNo != other.homeNo)
        return false;
    if (neighborhood == null) {
        if (other.neighborhood != null)
            return false;
    } else if (!neighborhood.equals(other.neighborhood))
        return false;
    if (street == null) {
        if (other.street != null)
            return false;
    } else if (!street.equals(other.street))
        return false;
    return true;
}
}
凉宸 2024-11-21 10:56:25

为 Address 对象定义/覆盖 equals() 和 hashcode()。比较如下:

address1.equals(address2)

Define/override equals() and hashcode() for the Address object. Compare as follows:

address1.equals(address2)
街道布景 2024-11-21 10:56:25

我会创建一个完全独立的类来进行比较,因为它们可能涉及特殊的逻辑和运行时配置。随着应用程序的增长,它可能涉及一些非常复杂的内容,例如匹配州(NYNew York)等。

不要使用 equals() 为此,应该是对 Java 集合和通用“这个对象是否完全相同”问题有意义的任何实现。

基本思想:

int studentId = /* something */;
Connection conn = /* something */;
AddressForm form = /* something */;
Address userEnteredAddress = form.getEnteredAddress();
Address storedAddress = Address.getAddress(connection,studentId);
MyAddressComparer comp = new MyAddressComparer(form);
boolean similarEnough = comp.doMyVagueComparison(storedAddress,userEnteredAddress);

AddressComparer 中的代码根据表单配置的用途来确定需要应用哪些规则,并执行所有这些特殊用途的小技巧。

I would create an entirely separate class to do the comparisons, because they probably involve special logic and run-time configuration. As your application grows, it might involve some very complex stuff like matching states (NY versus New York), etc.

Do not use equals() for this, which should instead be whatever implementation makes sense for Java collections and general-purpose "is this object exactly the same or not" questions.

Basic idea:

int studentId = /* something */;
Connection conn = /* something */;
AddressForm form = /* something */;
Address userEnteredAddress = form.getEnteredAddress();
Address storedAddress = Address.getAddress(connection,studentId);
MyAddressComparer comp = new MyAddressComparer(form);
boolean similarEnough = comp.doMyVagueComparison(storedAddress,userEnteredAddress);

It's up to the code in AddressComparer to figure out what rules it needs to apply based on what the form was configured to do, and to do all those little special purpose tricks.

九歌凝 2024-11-21 10:56:25

我只是调用这个伪代码,因为我不了解 Java:

if (this.gevernate != other.gevernate && this.gevernate != null && other.gevernate != null)
{
    // If both "gevernate" members are valid, but unequal...
    return false;
}

else if (this.homeNo != other.homeNo && this.homeNo != null && other.homeNo != null)
{
    // Repeat for "homeNo" values.
    return false;
}

// Repeat for the other two members.

else return true;

I'll just call this pseudocode, because I don't know Java:

if (this.gevernate != other.gevernate && this.gevernate != null && other.gevernate != null)
{
    // If both "gevernate" members are valid, but unequal...
    return false;
}

else if (this.homeNo != other.homeNo && this.homeNo != null && other.homeNo != null)
{
    // Repeat for "homeNo" values.
    return false;
}

// Repeat for the other two members.

else return true;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文