简体   繁体   English

Java泛型-编译器错误

[英]Java generics - compiler error

What's wrong with this method definition? 此方法定义有什么问题?

public static List<T extends MyObject> T find() {

}

Compiler says: 编译器说:

Syntax error, insert ";" to complete MethodDeclaration

You have two return types there. 您在那里有两种返回类型。

If you wanted to introduce a generic type T that would be 如果您想引入通用类型T

 public static <T extends MyObject>  List<T> find() {}

The proper method declaration would be: 正确的方法声明为:

public static <T extends MyObject> List<T> find() { ... }

When creating (static) generic methods, the generic parameter(s) has to be defined before the return-type, because they may be used in the return-type. 创建(静态)泛型方法时,必须在返回类型之前定义泛型参数,因为它们可以在返回类型中使用。

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

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