0

我有这个界面

/**
 * 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 实现另一个接口?

4

2 回答 2

2
<T extends MyRequisiteType> void exportData(IDataProvider<T> dataProvider, List<IExportableColumn<T,?,?>> columns, OutputStream outputStream) throws IOException;

为什么有 2 个无界类型参数IExportableColumn<T,?,?>?当我在应用程序代码中看到这一点时,我总是觉得我错过了一些东西。

您也可以使整个界面参数化,但据我所知,参数的数量需要非常大。至少3个。

于 2013-08-29T16:25:04.850 回答
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> { . . . }
于 2013-08-29T16:23:34.403 回答