简体   繁体   English

在Java中使用其他方法签名

[英]Using a different method signature in Java

The following two methods compiles fine and do what they stand for. 以下两种方法可以很好地编译,并能代表它们的意思。

public int returnArray()[]
{
    int a[]={1 ,2};
    return a;
}

public String[] returnArray(String[] array[])[]
{
    return array;
}

According to this method signature, can't we somehow have a method signature like the following? 根据此方法签名,我们不能以某种方式具有如下所示的方法签名吗?

public <T>List rerurnList(List<T> list)<T>
{
    return new ArrayList<T>();
}

This method is intended to return a java.util.List of generic type. 此方法旨在返回泛型类型的java.util.List It does not compile . 它不会编译 It must be modified as follows for its successful compilation. 为了成功编译,必须对其进行如下修改。

public <T>List<T> rerurnList(List<T> list)
{
    return new ArrayList<T>();
}

Can't we have a method signature like the first cases, in this case? 在这种情况下,我们不能像第一种情况那样具有方法签名吗?

For some reason Java lets you define arrays like in C, adding the [] modifier after the variable or method name. 出于某种原因,Java允许您像在C语言中那样定义数组,在变量或方法名称添加[]修饰符。 That, however, is not possible with generics. 但是,使用泛型是不可能的。

Generic type arguments have to be declared right with the type, because they are part of the type descriptor . 泛型类型参数必须与正确的类型声明,因为它们的类型描述符的一部分 Arrays should also be declared that way, as they are also part of the type descriptor . 数组也应该这样声明,因为它们也是类型描述符的一部分

In order to understand why the compiler does not let you write things that way (and why it shouldn't let you write things like in the first examples), we need to break it down to pieces. 为了理解为什么编译器不允许您以这种方式编写东西(以及为什么它不允许您像第一个示例中那样编写东西),我们需要将其分解为几部分。


public int returnArray()[] { ... }
  • public : Visibility declaration public :公开声明
  • int : Return type, integer int :返回类型,整数
  • returnArray : Method name returnArray :方法名称
  • () : Argument list (empty) () :参数列表(空)
  • [] : Whoops! [] :哎呀! the return type is actually an arrayof what we said before 返回类型实际上是我们之前所说的数组

This is even better: 这样更好:

public String[] returnArray(String[] array[])[]
  • public : Visibility declaration public :公开声明
  • String[] : Return type, an array of strings String[] :返回类型,字符串数组
  • returnArray : Method name returnArray :方法名称
  • (String[] array[]) : Argument list... (String[] array[]) :参数列表...
    • String[] : Type of the argument, array of strings String[] :参数的类型,字符串数组
    • array : Name of the argument array :参数名称
    • [] : Whoops! [] :哎呀! argument type is actually an array of what we said before 参数类型实际上是我们之前所说的数组
  • [] : Whoops again! [] :再次糟糕! return type is actually an array of what we said before 返回类型实际上是我们之前所说的数组

Foot note: Don't do this, specify the types only in the types. 脚注:请勿执行此操作,仅在类型中指定类型。 Instead of String[] array[] , use String[][] array . 代替String[] array[] ,使用String[][] array


Now that the array thing syntax is clear, and I hope you understand why it should be wrong, let begin with the generig thing: 现在,数组事物的语法已经很清楚了,希望您理解为什么它应该是错误的,让我们从一般事物开始:

public <T> List<T> rerurnList(List<T> list) { ... }
  • public : Visibility declaration public :公开声明
  • <T> : This method uses generic type T <T> :此方法使用通用类型T
  • List<T> : Return type, a generic List of T List<T> :返回类型, T的通用List
  • rerurnList : Method name rerurnList :方法名称
  • (List<T> list) : Argument list (List<T> list) :参数列表
    • List<T> : Argument type, generic List of T List<T> :参数类型, T通用List
    • list : Argument name list :参数名称

Just to answer your question: the syntax you're trying for the method that fails compilation is definitely wrong because of the <T> placed between the parameter list and the method body: 只是为了回答您的问题:由于参数列表和方法主体之间放置了<T> ,因此您为失败的方法尝试使用的语法肯定是错误的:

(List<T> list)<T>{

This is simply not valid Java syntax. 这根本不是有效的Java语法。 That's not how you mark a generic method. 这不是标记通用方法的方式。 You already marked the method as generic by putting the type parameter - <P> - between the method access modifier and its return type. 通过将类型参数- <P> -放在方法访问修饰符及其返回类型之间,您已经将该方法标记为通用。

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

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