Answer the question
In order to leave comments, you need to log in
How compares java objects when using lombok?
Good day to all! I encountered strange behavior, I can’t understand what’s wrong, in theory the code is similar, but it works differently.
There is an abstract class AbstractCat
package animal;
import lombok.Data;
@Data
public abstract class AbstractCat {
private String name;
private Integer age;
}
package animal;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class Cat extends AbstractCat {
private String say;
}
package transport;
import lombok.Data;
@Data
public abstract class AbstractTransport {
private Integer rnum;
private String rtype;
}
package transport;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class Transport extends AbstractTransport {
private Integer arrt;
private String where;
private String vehid;
private Integer rid;
private String lastst;
}
import animal.AbstractCat;
import animal.Cat;
import transport.AbstractTransport;
import transport.Transport;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
animal();
System.out.println();
transport();
}
private static void animal() {
AbstractCat abstractCat = new AbstractCat() {
@Override
public void setName(String name) {
super.setName(name);
}
@Override
public void setAge(Integer age) {
super.setAge(age);
}
};
abstractCat.setAge(1);
abstractCat.setName("Cat");
Cat cat = new Cat();
cat.setAge(1);
cat.setName("Cat");
cat.setSay("Hello");
System.out.println("abstractCat.equals(cat) " + abstractCat.equals(cat));
}
private static void transport() {
AbstractTransport abstractTransport2 = new AbstractTransport() {
@Override
public void setRnum(Integer rnum) {
super.setRnum(rnum);
}
@Override
public void setRtype(String rtype) {
super.setRtype(rtype);
}
};
abstractTransport2.setRnum(33);
abstractTransport2.setRtype("М");
Transport transport1 = new Transport();
transport1.setRnum(33);
transport1.setRtype("M");
transport1.setWhere("dudl");
System.out.println("abstractTransport.equals(transport1) " + abstractTransport2.equals((Transport)transport1));
System.out.println("transport1.equals(abstractTransport) " + transport1.equals(abstractTransport2));
}
}
abstractCat.equals(cat) true
abstractTransport.equals(transport1) false
transport1.equals(abstractTransport) false
└── src
├── main
│ ├── java
│ │ ├── animal
│ │ │ ├── AbstractCat.java
│ │ │ └── Cat.java
│ │ ├── Main.java
│ │ └── transport
│ │ ├── AbstractTransport.java
│ │ └── Transport.java
│ └── resources
└── test
├── java
└── resources
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question