首页 \ 问答 \ 覆盖java ... Object类中的equals方法(Overriding an equals method from the java…Object class)

覆盖java ... Object类中的equals方法(Overriding an equals method from the java…Object class)

我一直在试图弄清楚这个问题背后的原因,我一直在努力去理解为什么结果是这样。 我会解释我理解的一切,希望有人能够填补我的空白。

想象一下你有一堂课:

public class Point {
    public boolean equals(Object o) {
        if (o == null || (!(o instanceof Point)) { // Let's call this method 1
            return false;
        }
        Point other = (Point) o;
        return x == other.x && y == other.y;
    }

    public boolean equals(Point p) { // Let's call this method 2
        if (p == null) {
            return false;
        }
        return x == p.x && y == p.y;
    }
}

现在我们创建以下对象:

Object o = new Object()

Point p = new Point(3,4)

Object op = new Point(3,4)

如果我们打电话:

p.equals(o) // this calls method 1

p.equals(p) // this calls method 2

p.equals(op) // this calls method 1

但是,这是我感到困惑的地方。

op.equals(o) // this calls method 1

op.equals(op) // this calls method 1

op.equals(p) // this calls method 1

为什么最后一个调用方法1? 方法2的方法签名不应该保证呼叫去那里吗?

如果任何人都可以向我解释这将是伟大的!


I've been attempting to figure out the rationale behind this question and I have been struggling to see why the outcome is what it is. I will explain everything I understand and I hope someone will be able to fill in the blanks for me.

Imagine you have a class:

public class Point {
    public boolean equals(Object o) {
        if (o == null || (!(o instanceof Point)) { // Let's call this method 1
            return false;
        }
        Point other = (Point) o;
        return x == other.x && y == other.y;
    }

    public boolean equals(Point p) { // Let's call this method 2
        if (p == null) {
            return false;
        }
        return x == p.x && y == p.y;
    }
}

Now we create the following objects:

Object o = new Object()

Point p = new Point(3,4)

Object op = new Point(3,4)

If we call:

p.equals(o) // this calls method 1

p.equals(p) // this calls method 2

p.equals(op) // this calls method 1

However, this is where I get confused.

op.equals(o) // this calls method 1

op.equals(op) // this calls method 1

op.equals(p) // this calls method 1

Why does the last one call method 1? Shouldn't the method signature of method 2 warrant the call to go there?

If anyone could explain it to me that would be great!


原文:https://stackoverflow.com/questions/39934067
更新时间:2023-11-30 21:11

最满意答案

最好的解决方案是不存储用户密码。 如果您使用护照,则应使用类似passport-twitter的内容,并存储Twitter用户ID。

如果您仍想存储密码,请使用salting对其进行哈希处理。 我建议河豚 ,我需要存储密码时使用它。


The best solution would be to not store user passwords. If you use passport, you should use something like passport-twitter, and store the Twitter user ID.

If you still want to store passwords, hash it with salting. I recommend blowfish, I use it when I need to store passwords.

相关问答

更多
  • MD5已被打破 。 SHA-1有很多弱点 。 SHA-2目前被认为是足够的。 SHA-3很快将成为FIPS标准 。 最佳实践是将密码散列与随机salting和键拉伸相结合,例如PBKDF2 。 关于密码腌制,散列和拉伸的良好讨论 。 我在C#中实现了密码salting,散列和拉伸 。 至于散列提供的额外安全性,这取决于您使用的散列迭代次数。 例如,假设您决定使用2 ^ 14个哈希迭代。 这会将密码的熵增加14位。 根据摩尔定律,哈希提供的每个额外的熵都意味着在今天的同一时间内破解密码大约需要18个月。 因此 ...
  • 我认为登录过程中存在问题。 您可以使用authenticate()函数作为路由中间件来验证请求。 喜欢这个: router.post('/login', passport.authenticate('local', { failureRedirect: '/login' }), function(req, res) { res.redirect('/'); }); I think there is a problem in Login process. You can use aut ...
  • 切勿存储加密密码。 存储安全的单向哈希,比如SHA-1 (有一些小的安全问题),或者更新,更安全的变体之一。 这样做实际上是针对您可能受到的若干监管要求,例如PCI DSS,如果您涉及信用卡(进行任何电子商务?)。 像http://www.mindrot.org/projects/jBCrypt/这样的东西也可能有用。 对于Borealid的评论+1 - 即使使用散列,散列也需要正确完成,并且必须包含“ salt ”(额外的随机数据以防止攻击的子集)。 jBCrypt会为你做这个(和其他类似的库一样)。 N ...
  • 存储散列密码时,空白删除不是问题: 您从不存储密码本身,而是存储哈希函数的输出。 此哈希函数生成固定大小的输出(或者您可能使用可逆加密算法)。 大多数哈希函数的输出已经是固定大小的明文[1] ,不包含任何空格。 否则,您可能正在使用不用于密码散列的散列函数。 固定大小的盐与哈希输出一起存储(密码哈希函数,它们知道盐析通常输出带有哈希密码的盐)。 如果您想要灵活并且能够在将来升级到更好的散列函数,您还必须记住使用了哪个散列函数。 建议使用明文[1]来获取此信息,这使得完整记录可变长度。 RFC 2307描述了 ...
  • 问题出在前端,我没有在正确的位置将Credentials设置为true var options = new RequestOptions({ headers: headers, withCredentials: true }); The issue was on the front end I was not setting withCredentials to true in the correct location var options = new RequestOptions( ...
  • 1)serializeUser正在过滤数据并将其存储在会话cookie中。 如果可以,通常在cookie中存储较少。 无论如何,您将调用数据库以获取有关用户的数据,因此您只需存储用于检索和重新构建用户的ID,如deserializeUser中所示。 来自cookie的请求由客户端提供给服务器,服务器将cookie反序列化为数据,解密cookie内容或从数据库检索用户数据。 然后响应出去了服务器序列化客户端数据,刮掉你不会存储在cookie中的东西并将它们放入数据库中,只需在cookie中留下一个id。 如果 ...
  • 最好的解决方案是不存储用户密码。 如果您使用护照,则应使用类似passport-twitter的内容,并存储Twitter用户ID。 如果您仍想存储密码,请使用salting对其进行哈希处理。 我建议河豚 ,我需要存储密码时使用它。 The best solution would be to not store user passwords. If you use passport, you should use something like passport-twitter, and store the ...
  • passport是一个身份验证库,会话是授权的一部分 - 这个过程中的一个不同步骤。 您可以使用passport来发出令牌,例如jsonwebtoken ,而不是使用会话,然后使用不同的库来验证诸如ejwt那些或创建中间件函数来验证需要身份验证的路由上的这些令牌 passport也是可扩展的 - 已经开发了许多插件,允许多种形式的身份验证来发布您的授权方法。 你不必使用它,它只是让它更容易一些。 另一方面,你可能不得不使用。 passport is an authentication library and ...
  • 虽然这不是你想要的答案,但你只有三种可能性 存储密码明文(呃!) 具有可逆加密的商店,例如RSA(http://stackoverflow.com/questions/4484246/encrypt-and-decrypt-text-with-rsa-in-php) 不要存放; 客户端只能重置密码,不能查看密码 第二种选择是安全的方式,因为RSA也用于您选择的银行使用的HTTPS协议中的TLS加密;) Though this is not the answer you were looking for, y ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)