有什么东西可以从不同实体类中获得一些属性吗?

时间:2019-06-06 07:52:42

标签: spring spring-mvc

例如ı拥有2个这样的实体类,例如想要获取第一个ID并命名第二个获得城市

@Id
private int id;

private String name;

private long university_id;

private String student_at;

另一个就像

@Id
@Column(name = "id")
private Long id;

@Column(name = "name")
private String name;

@Column(name = "city")
private String city;

我试图建立一个界面

public interface getsomething{
    String getName();
    int getId();
   Object object = new Object();(think that object is the other class name)
   String object.getName();
}

和其他东西不同,但是那是行不通的?我需要做什么

1 个答案:

答案 0 :(得分:0)

似乎您要检查类的类型,然后根据类名称执行 您可以将界面设计为

public interface getsomething(Object object){

 String name ;
 int id;
 String entityClass = object.getClass().getName();
 if(entityClass.equals("Entity1")) {
   name = ((Entity1)object).getName();
 } else {
   id = ((Entity2)object).getId();
 }
}

让我知道是否有帮助。