本文介绍了工厂和仿制药的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

I have the following classes:

 code>对象,因此可以捕获 T 。但是您不得不在运行时检查 Class 来确定要返回哪个 IDataSource< T> 。此时,类型擦除早已发生。

You are passing in a Class object so T can be captured. But you are forced to check the Class at runtime to determine which IDataSource<T> to return. At this time, type erasure has long since occurred.

在编译时,Java无法确定类型安全性。它不能保证在运行时 Class 中的 T 是相同的 T 在 IDataSource< T> 中返回,所以它会产生警告。

At compile time, Java can't be sure of type safety. It can't guarantee that the T in the Class at runtime would be the same T in the IDataSource<T> returned, so it produces the warning.

看起来像是当你被迫使用 @SuppressWarnings(unchecked)注释该方法来删除警告的时候。这种警告是有原因的,所以您要提供并确保类型安全。正如所写的,它看起来像你提供了类型安全。

This looks like one of those times when you're forced to annotate the method with @SuppressWarnings("unchecked") to remove the warning. That warning is there for a reason, so it is up to you to provide and ensure type safety. As written, it looks like you have provided type safety.

@SuppressWarnings("unchecked")
public static <T> IDataSource<T> getDataSource(Class<T> dataType) {

这篇关于工厂和仿制药的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:58