通用方法语法

时间:2012-03-30 11:00:28

标签: java

我忘记了泛型方法的语法:

public static void swap <T> (T a, T b)
{...}

我无法在任何地方找到一些例子。什么是正确的语法?

3 个答案:

答案 0 :(得分:1)

Here I have written a sample piece which i have used for my application. You can get an idea from this.


        public <C>void addColumn(Cell<C> cell, String headerText,
              final GetValue<C> getter, FieldUpdater<DocumentType, C> fieldUpdater) {
            Column<DocumentType, C> column = new Column<DocumentType, C>(cell) {
              @Override
              public C getValue(DocumentType object) {
                return getter.getValue(object);
              }
            };
            column.setFieldUpdater(fieldUpdater);
            column.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
            getTable().addColumn(column, headerText);
            getTable().setColumnWidth(column, 10, Unit.PX);
    }

答案 1 :(得分:1)

static <T> void swap(T a, T b) {..}

答案 2 :(得分:1)

您必须在返回类型之前声明您输入的参数(在您的情况下为void)。

http://docs.oracle.com/javase/specs/jls/se5.0/html/classes.html#8.4(见TypeParameters