Привет Я пытаюсь переопределить метод равных и hashcode для моего пользовательского класса.overriding equal и hashcode в Java, где this.obj равно null
вот мой собственный класс
public class property
{
public String type = null;
public int value;
public int id;
public String name = null;
public String course = null;
public property(String type, String course, int value, int id)
{
this.value = value;
this.type = type;
this.course = course;
this.id = id;
}
@Override
public boolean equals(Object obj) {
if(this == obj)
return true;
if((obj == null) || (obj.getClass() != this.getClass()))
return false;
property sig = (property) obj;
if(sig != null) {
if(sig.type.equals("EST1")) {
if (sig.name != null && sig.type != null && sig.course != null) {
Log.d("property", "Other " + sig.course + "AHHAHAHA " + course + "shahahaaha " + sig.id + "babababaab " + id);
if (sig.course.equals(this.course) && sig.value == this.value && sig.type.equals(this.type) && sig.id == (this.id)) {
Log.d("property", "EST1");
return true;
}
}
}
return false;
}
Проблема заключается в том, что «this.id, this.type» всегда нуль или 0 я не понимаю, почему ... в то время как sig.id, sig.type имеют значения. Просто чтобы вы знали, что это всего лишь фрагмент кода, а не полный код, поэтому он может иметь опечатки и не логически исправлять.
'this.id' не может быть пустым, это может быть ** 0 ** –
да, как я сказал могут быть опечатки ... извините – user3290805
Можете ли вы опубликовать код, из которого вы на самом деле вызываете метод 'equals' !! –