覆盖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
最满意答案
最好的解决方案是不存储用户密码。 如果您使用护照,则应使用类似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.
相关问答
更多-
有什么数据库专业书籍介绍?[2022-05-19]
mysql -
MD5已被打破 。 SHA-1有很多弱点 。 SHA-2目前被认为是足够的。 SHA-3很快将成为FIPS标准 。 最佳实践是将密码散列与随机salting和键拉伸相结合,例如PBKDF2 。 关于密码腌制,散列和拉伸的良好讨论 。 我在C#中实现了密码salting,散列和拉伸 。 至于散列提供的额外安全性,这取决于您使用的散列迭代次数。 例如,假设您决定使用2 ^ 14个哈希迭代。 这会将密码的熵增加14位。 根据摩尔定律,哈希提供的每个额外的熵都意味着在今天的同一时间内破解密码大约需要18个月。 因此 ...
-
护照登录 - mysql(Passport login - mysql)[2022-03-25]
我认为登录过程中存在问题。 您可以使用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 ...
-
将哈希密码存储到Mysql(store hashed passwords to Mysql)[2023-05-08]
存储散列密码时,空白删除不是问题: 您从不存储密码本身,而是存储哈希函数的输出。 此哈希函数生成固定大小的输出(或者您可能使用可逆加密算法)。 大多数哈希函数的输出已经是固定大小的明文[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.js?(When to use passport.js?)[2021-10-17]
passport是一个身份验证库,会话是授权的一部分 - 这个过程中的一个不同步骤。 您可以使用passport来发出令牌,例如jsonwebtoken ,而不是使用会话,然后使用不同的库来验证诸如ejwt那些或创建中间件函数来验证需要身份验证的路由上的这些令牌 passport也是可扩展的 - 已经开发了许多插件,允许多种形式的身份验证来发布您的授权方法。 你不必使用它,它只是让它更容易一些。 另一方面,你可能不得不使用。 passport is an authentication library and ... -
存储MySQL密码[关闭](Storing MySQL Passwords [closed])[2022-05-05]
虽然这不是你想要的答案,但你只有三种可能性 存储密码明文(呃!) 具有可逆加密的商店,例如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 ...