以下是Java中通用类的代码段
public class PairTest{
public static void main(String[] args){
LocalDate[] birthdays = {....} // initialize birthdays array
Pair<LocalDate> mm = ArrayAlg.minmax(birthdays); // THIS LINE !
}
}
public ArrayAlg{
public static < T extends Comparable> Pair<T> minmax(T[] a){
T min = a[0];
T max = a[0];
for(int i = 0; i < a.length; i++){
if(min.compareTo(a[i] > 0) min = a[i];
if(max.compareTo(a[i] < 0) max = a[i];
}
return new Pair<>(min,max);
}
}
我的问题是,注释为THIS LINE !
的行确实告诉静态方法minmax的泛型类型,所以我应该将此行解释为Pair<LocalDate> mm = ArrayAlg.<LocalDate>minmax(birthdays);
。
或者泛型方法类型基于我传入的参数类型是&#39; a&#39;,例如,如果是ArrayAlg.minmax(SubOfLocalDate[] a)
,它会为该静态方法返回new Pair<SubOfLocalDate> (min, max)
。
答案 0 :(得分:2)
如果您的问题是基于什么原因,编译器会使用泛型方法的类型参数,那么它是基于方法参数birthdays
的声明类型LocalDate[]
类型T
}。因此,类型参数LocalDate
绑定到Comparable
(实现T
,因此它匹配mm
的上限。)
请注意,在这种情况下,编译器不需要检查已分配变量mm
的声明类型以确定绑定类型参数。如果使用其他类型声明分配的变量,即如果Pair<SomeOtherType>
类型为289,112,287,112,286,110,285,110,282,109,280,109,278,109,275,108,273,108,271,106,269,106,266,106,264,106,261,105,258,105,257,105,255,105,254,104,253,104,251,104,249,104,248,104,245,104,244,104,240,104,238,104,235,104,232,104,228,106,226,106,222,107,221,108,216,111,214,111,211,114,209,115,205,118,201,121,198,123,195,126,191,129,189,131,185,135,184,136,180,140,178,143,176,147,174,151,171,155,171,157,168,162,167,165,166,170,166,174,165,179,165,185,165,188,165,192,165,195,165,198,165,201,168,204,169,207,172,211,176,216,177,219,181,223,185,226,188,230,193,234,198,237,204,241,210,244,216,247,223,249,230,252,237,254,244,256,251,257,260,258,268,260,276,260,283,262,290,262,296,262,300,262,306,262,312,262,315,262,319,262,321,260,326,258,330,254,333,251,337,244,339,241,342,235,344,230,345,225,348,219,346,179,342,173,340,170,337,165,336,163,334,161,332,159,330,156,328,154,326,153,325,151,323,150,321,147,320,146,319,145,316,143,314,142,311,140,310,139,308,139,307,137,305,136,304,135,302,135,302,134,300,133,300,132,298,130,297,130,297,129,295,128,295,127,294,127,294,126,293,125,292,125,292,124,291,123,290,122,289,121,288,120,287,120,287,119,286,119
254,116,253,116,252,116,251,116,250,116,247,116,244,117,238,120,234,120,229,123,223,126,217,131,210,137,204,141,198,145,193,149,191,152,187,155,182,159,181,161,179,164,178,166,177,170,176,172,175,178,174,182,174,186,173,189,173,195,173,200,173,204,173,208,173,214,173,220,177,224,180,230,184,234,189,239,192,242,197,244,205,247,211,249,217,252,227,254,235,257,242,258,251,259,258,259,267,259,274,259,281,259,286,259,292,259,296,259,301,258,307,255,312,251,318,246,323,238,330,231,334,226,336,220,339,213,340,209,340,205,341,198,341,191,341,184,341,178,341,171,341,166,340,162,338,158,337,156,336,152,334,151,334,149,331,147,330,146,328,143,326,142,325,141,323,140,320,138,318,138,317,137,315,136,311,135,309,134,307,133,306,132,303,132,302,131,299,131,298,130,295,130,292,129,290,127,287,127,286,127,285,127,283,126,282,126,281,125,280,125,279,125,278,125,277,125,276,125,274,124,272,124,271,123,269,123,267,123,265,123,264,123,263,123,261,122,260,122,259,122,258,122,258,121,257,121,257,120
301,143,300,143,299,143,297,143,294,141,291,140,289,139,286,139,282,139,277,136,272,135,268,135,262,134,258,134,252,132,248,132,244,132,239,132,234,132,228,132,222,132,218,132,211,132,206,132,199,132,195,133,188,136,184,137,180,139,177,140,173,142,171,143,169,145,167,147,166,151,163,155,162,158,159,165,157,172,155,178,155,185,155,192,155,200,155,206,155,214,156,220,160,227,166,236,171,243,176,250,184,257,190,263,197,269,207,275,214,280,223,284,230,287,240,290,249,292,257,294,265,294,274,295,279,295,286,295,292,295,300,294,305,292,312,289,318,287,326,282,333,277,340,271,345,266,351,261,356,258,360,253,364,249,365,246,366,243,369,239,369,236,369,232,369,228,369,224,369,221,369,217,369,212,368,209,367,208,366,204,364,200,363,198,360,194,359,191,357,187,356,185,354,182,352,180,351,178,350,177,348,173,347,173,346,172,344,172,342,169,341,169,339,169,337,168,335,166,334,166,330,165,329,164,327,163,325,162,323,161,320,159,318,159,316,158,314,158,313,157,311,157,311,156,310,155,309,155,307,155,307,154,306,154,305,153,304,153,303,152,303,151,302,151,301,151,300,150,299,148,298,148,297,147
,则编译器将生成错误。
答案 1 :(得分:2)
在运行时
public static <T extends Comparable<T>> Pair<T> minmax(T[] a) {
变为类型擦除
public static Pair<Comparable> minmax(Comparable[] a) {
和
Pair<LocalDate> mm = ArrayAlg.minmax(birthdays);
实际上只是
Pair mm = ArrayAlg.minmax(birthdays);