gpt4 book ai didi

java - 从父类(super class)的属性检索数据的通用方法

转载 作者:行者123 更新时间:2023-12-01 11:13:30 25 4
gpt4 key购买 nike

我有一些使用通用 BaseEntity 扩展的实体,这些实体具有 Id 属性,我想要执行一个方法来传递实体并检索该 id,即

基础实体

public abstract class BaseEntity implements Serializable {

@Id
@GeneratedValue
private Long id;
// Getters and setters
}

实体

public class Entidad extends BaseEntity {

protected String name;

protected String image;

protected Set<ContactForm> contactForms = new HashSet<ContactForm>();

}

(ContactForm 也继承自 BaseEntity)我想做这样的事情:

List<Long> longlist;

longlist = retrieveIds(entidad.getContactForms);

[...]

public List<Long> retrieveIds(Set<BaseEntity> entidades){
List<Long> lista = new ArrayList<Long>();

for(BaseEntity entidad : entidades){
lista.add(entidad.getId());
}

return lista;
}

这可能吗?怎么办?

最佳答案

是的。这是可能的。

只需在您的父类(super class)中创建一个方法,并在返回该类的 id 的所有子类中覆盖该方法。

关于java - 从父类(super class)的属性检索数据的通用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32112618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com