我是Java 的新手,我想收集一些 idea ,因为我搞砸了.
我有一些家长班,比方说
@Data
public class Person implements Hometown {
private String name;
private Country motherland;
}
一个接口:
public interface Hometown {
Country getMotherland();
}
以及一些 children 班级:
@Data
public class Norwegian extends Person {
//motherland should be "Norway"
..
}
@Data
public class Portuguese extends Person {
//motherland should be "Portugal"
..
}
通过RestClient,我可以接收Person类:
public interface PopulationInfoClient {
@GET
@Path("/people/{personId}")
Person search(@PathParam("personId") String personId);
}
在我收到个人信息后,我想为他们建立一个家乡,为我服务.然后返回泛型类型的子类(可以是挪威语、葡萄牙语等).
@Inject
@RestClient
PopulationInfoClient populationInfoClient;
public T getCitizen(String personId) {
Person person = this.populationInfoClient.search(personId);
...
return ...
}
问题是,我如何使用泛型创建像挪威人这样的 children 类,而祖国被设置为"挪威"?并在我的服务函数getCitizen()中返回它们?
如果能给我答案和 idea ,我将不胜感激
我try 使用工厂模式,但失败了