简体   繁体   English

通用方法语法

[英]Generic method syntax

I have forgotten the syntax for a generic method: 我忘记了泛型方法的语法:

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

And I don't find anywhere some example.What's the correct syntax? 我找不到任何一个例子。什么是正确的语法?

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);
    }
static <T> void swap(T a, T b) {..}

You have to declare your typed parameters before the return type (void in your case). 您必须在返回类型之前声明您键入的参数(在您的情况下为void)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM