[英]How to determine the class of the object extending an abstract class
I am trying to place common functionality for JPA entities in an Abstract class: 我试图将JPA实体的通用功能放在Abstract类中:
@MappedSuperclass
public abstract class AbstractEntity implements java.io.Serializable {
private static final Logger log = LoggerFactory.getLogger(ConcreteClass.class);
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private UUID uuid;
@Override
public int hashCode() {
return getUuid().hashCode();
}
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (!(obj instanceof ConcreteClass))
return false;
return getUuid().equals(((ConcreteClass) obj).getUuid());
}
Is this possible / useful? 这可能/有用吗? How does the Abstract class figure out the concrete class?
Abstract类如何找出具体的类?
Referencing comments below (working code): 引用下面的注释(工作代码):
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj instanceof AbstractEntity && this.getUuid().equals(((AbstractEntity)obj).getUuid()))
return true;
return false;
}
You don't. 你不知道 All you really care about here is
您真正关心的是
obj instanceof AbstractEntity && this.getUuid().equals(((AbstractEntity)obj).getUuid())
and maybe 有可能
this.getClass() == obj.getClass()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.