我有这个界面
/**
* Exports data provided by a {@link IDataProvider} as described by {@link IExportableColumn}s. This interface is used by
* {@link ExportToolbar} to provide the export functionality.
*
* @author Jesse Long
* @see ExportToolbar
* @see IExportableColumn
*/
public interface IDataExporter
extends IClusterable
{
...
/**
* Exports the data provided by the {@link IDataProvider} to the {@link OutputStream}.
*
* @param <T>
* The type of each row of data provided by the {@link IDataProvider}.
* @param dataProvider
* The {@link IDataProvider} from which to retrieve the data.
* @param columns
* The {@link IExportableColumn} to use to describe the data.
* @param outputStream
* The {@link OutputStream} to which to write the exported data.
* @throws IOException If an error occurs.
*/
<T> void exportData(IDataProvider<T> dataProvider, List<IExportableColumn<T, ?, ?>> columns, OutputStream outputStream)
throws IOException;
}
在类中以某种方式实现此接口我可以将T键入特定的内容,我想确保T实现另一个接口吗?
答案 0 :(得分:2)
<T extends MyRequisiteType> void exportData(IDataProvider<T> dataProvider, List<IExportableColumn<T,?,?>> columns, OutputStream outputStream) throws IOException;
为什么IExportableColumn<T,?,?>
中的2个无界类型参数?当我在应用程序代码中看到这一点时,我总是觉得我错过了一些东西。
你也可以使整个界面参数化,但从我看到的参数数量需要相当大。至少3。
答案 1 :(得分:1)
如果您总是想强制T
实现某个界面,请使用
<T extends Thing> void exportData(. . .)
如果只想在实现类中限制T
,可以使整个接口通用(而不仅仅是方法)。无论如何,这可能是一个好主意:
/**
* Exports data provided by a {@link IDataProvider} as described by {@link IExportableColumn}s. This interface is used by
* {@link ExportToolbar} to provide the export functionality.
* @param <T>
* The type of each row of data provided by the {@link IDataProvider}.
*
* @author Jesse Long
* @see ExportToolbar
* @see IExportableColumn
*/
public interface<T> IDataExporter extends IClusterable
{
...
/**
* Exports the data provided by the {@link IDataProvider} to the {@link OutputStream}.
*
* @param dataProvider
* The {@link IDataProvider} from which to retrieve the data.
* @param columns
* The {@link IExportableColumn} to use to describe the data.
* @param outputStream
* The {@link OutputStream} to which to write the exported data.
* @throws IOException If an error occurs.
*/
void exportData(IDataProvider<T> dataProvider, List<IExportableColumn<T, ?, ?>> columns, OutputStream outputStream)
throws IOException;
}
然后你可以在实现类声明中绑定T
:
public class MyClass implements IDataExporter<MyRowType> { . . . }