简体   繁体   English

泛型方法:静态与非静态

[英]Generics methods: static vs NON-static

Directly from this java tutorial: 直接来自这个 java教程:

For static generic methods, the type parameter section must appear before the method's return type. 对于静态泛型方法,类型参数部分必须出现在方法的返回类型之前。

Is it not true for NON-static generic method? 对于非静态通用方法不是这样吗? If not what's the NON-static generic method syntax? 如果不是什么是非静态泛型方法语法? Thanks in advance. 提前致谢。

The syntax for declaring non-static generic methods is the same as for static methods, just without the static keyword: generic type parameters are placed before the return type. 声明非静态泛型方法的语法与静态方法相同,只是没有static关键字:泛型类型参数放在返回类型之前。

class Example {
     public <E> void method(E param) { }
}

Non-static methods may also use the generic type parameter of the enclosing class, like below. 非静态方法也可以使用封闭类的泛型类型参数,如下所示。 These are not considered generic methods; 这些不被视为通用方法; a generic method is one that declares type parameters . 泛型方法是声明类型参数的方法

class Example<T> {
     // Not a generic method!
     public void method(T param) { }
}

This is true for any generic methods. 对于任何通用方法都是如此。

public <T> T f() {
    return this.<T> f();
}

该语句适用于所有泛型方法,因为这是泛型方法的定义 - 泛型方法是声明类型参数的方法。

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

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