将一个类分配给2个变量并比较它们

huangapple go评论78阅读模式
英文:

Assigning one class to 2 variables and comparing them

问题

抱歉,如果这个问题之前已经被提出过,但我想知道:

假设我创建了一个类的新实例并将其赋值给一个变量,例如:MyClassName myclass = new MyClassName(一些参数);,然后我将它赋值给另一个变量或者将其放入一个列表中,例如:list.add(myclass);。如果我在一个if语句中比较这两者,if (list.get(0).equals(myclass),它会返回true吗?

如果问题有点模糊或不清楚,请原谅。

英文:

Sorry if this has been asked before but I wanted to know:

Say i make a new instance of a class and assign it to a variable e.g.: MyClassName myclass = new MyClassName(some parameters); and I assign this to another variable or put it in a List e.g.: list.add(myclass);. If I compare the two in an if clause if (list.get(0).equals(myclass) would it return true?

Forgive me if the question might be a bit vague or unclear.

答案1

得分: 0

两种在Java中匹配对象的方法:

  1. 匹配一个对象与另一个对象,例如:obj1 == obj2
  2. 另一种方法是使用obj1.equals(obj2).... 在这种情况下,obj1必须有一个名为equals的方法...

现在,在第二种情况下会发生什么:equals方法在大多数情况下是自定义设计的,即您必须提供它。而在某些情况下(对于内置类),它由Java提供,例如对于String。因此,我希望您明白obj1 == obj2不会调用equals方法。这是因为Java没有像C++中的“运算符重载”之类的花哨功能。因此,equals方法通常用于检查两个对象的内容是否相同。

那么,当您执行obj1 == obj2时会发生什么?它只是检查obj1的对象引用是否与obj2相同。这意味着它们是否都指向相同的内存。因此,有可能obj1 == obj2返回false,即使它们的内容相同。

让我给您展示一个示例:

class SomeClass {
  private int x;
  public SomeClass(int x) {
    this.x = x;
  }
  public boolean equals(SomeClass o) {
    return this.x == o.x;
  }
}

现在,编写一些测试:

SomeClass a = new SomeClass(12);
SomeClass b = new SomeClass(12);
SomeClass c = a;

System.out.println(a == c); // 输出 true
System.out.println(a == b); // 输出 false

System.out.println(a.equals(c)); // 输出 true
System.out.println(a.equals(b)); // 输出 true

希望这清楚了一切。如果有任何不清楚的地方,请在评论中询问我。

英文:

Two ways of matching object in java:

  1. matching one object's with another object like: obj1 == obj2
  2. another one is obj1.equals(obj2)....in this case obj1 must have a methods called equals...

Now, what happens in the second case: equals method is custom design in most cases, i.e. you've to provide it. And in some cases (for builtin classes), it's provided by java, for example for String. So, I hope you understood that obj1 == obj2 does not call on the equals method. That is because java does not have any fancy thing like operator overriding as in C++. So, the equals method is generally used to check if two objects content are same or not.

So, what happens when you do obj1 == obj2? It just checks if obj1's object reference is the same or not as obj2. That means they both target the same memory or not. So, it is possible that, obj1 == obj2 may return false even if there contents are same.

Let me show you an example:

class SomeClass {
  private int x;
  public SomeClass(int x) {
    this.x = x;
  }
  public boolean equals(SomeClass o) {
    return this.x == o.x;
  }
}

Now, write some tests:

SomeClass a = new SomeClass(12);
SomeClass b = new SomeClass(12);
SomeClass c = a;

System.out.println(a == c); // print true
System.out.println(a == b); // print false

System.out.println(a.equals(c)); // print true
System.out.println(a.equals(b)); // print true

Hope this clears everything. If anything is unclear ask me in the comment.

huangapple
  • 本文由 发表于 2020年8月12日 03:24:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63365058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定