因此,与我合作的团队减少了我们为某些事情输入的代码量。在本例中,一个使用 DisplayTag 库显示列表的 Spring 网页。它的完成方式是使用一个类,该类使用泛型扩展 Controller
对象,然后为它应该处理的每个页面创建一个子类。
此 Controller 显示 SystemErrorReport
,并将类型定义为 SystemErrorReportCommand
。
public class SystemErrorReportController extends
GenericSearchAndSelectController<SystemErrorReportCommand> {
问题是作为类型传递的 SystemErrorReportCommand
需要在其构造函数中手动声明自身,如下所示:
public SystemErrorReportCommand()
{
super(SystemErrorReport.class);
}
命令对象随后被传递给需要知道其显式类型的对象。没有在某处手动指定它,它作为我们的另一个类 GenericCommand
返回,因为 SystemErrorReportCommand
位在编译时间后丢失。
我对此并不感到兴奋,因为这似乎是我们可以进一步自动化以减少开发人员错误的事情。是否有更优雅的方法来执行此操作,或者我是否因为类型删除而坚持使用此方法?
最佳答案
与广为接受且鲜为人知的类型删除相反,可以避免类型删除,这意味着被调用方确实有能力知道在调用期间使用了哪些通用参数。
请看: Using TypeTokens to retrieve generic parameters
谢谢
关于java - 绕过 Java 中的类型删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1509896/