gpt4 book ai didi

java - 子类型 "inherit"通用接口(interface)是否使用该特定子类型 (Java) 进行参数化?

转载 作者:搜寻专家 更新时间:2023-11-01 03:02:28 25 4
gpt4 key购买 nike

我有

class Shape implements Comparable <Shape>

class Square extends Shape


我写了一个通用方法来查找数组中的最大元素:

public static <S extends Comparable<S>> S findMax(S[] arr)
{
//blablabla...
return maxS;
}

这两个调用没有给我任何错误并且做了它们应该做的事情:

Shape maxShape = findMax(new Shape[]{new Shape(1), new Shape(2), new Shape(3)});
Square maxSquare = findMax(new Square[]{new Square(1), new Square(2), new Square(3)});

因此,在我看来,自 Shape 以来似乎是合理的工具 Comparable<Shape>Square延伸Shape , Square s 也应该是可比较的,又名 Square以某种方式自动实现 Comparable<Square>通过继承(特别是通过继承 compareTo(Shape s) )。

然而,根据我的教科书,情况并非如此:“我们只知道 Square implements Comparable<Shape> ;因此 Square IS-A Comparable<Shape> ,但 IS-NOT-A Comparable<Square> ”,它建议使用更好的方法签名:
public static <S extends Comparable<? super S>> .

那为什么我的public static <S extends Comparable<S>>给我没问题?

----------------------------更新(源代码)------------ ------------------

public class Shape implements Comparable<Shape>{
protected int area;
public Shape (int i)
{
this.area=i;
}

public String toString()
{
return area+"";
}

public static void main(String[] args)
{

System.out.println("Bigger shape: "+findMax(new Shape[] {new Shape(2),new Shape(3)}));
System.out.println("Bigger square: "+findMax(new Square[] {new Square(2),new Square(3)}));

}

public int getValue()
{
return area;
}

@Override
public int compareTo(Shape sh) {
return Integer.valueOf(area).compareTo(sh.getValue());
}

public static <N extends Comparable<N>> N findMax(N[] arr)
{
int maxIdx=0;
for (int i=1; i<arr.length; i++)
if (arr[i].compareTo(arr[maxIdx])>0)
maxIdx=i;
return arr[maxIdx];
}

}

class Square extends Shape
{
public Square(int i)
{
super(i);
}

public int compareTo(Shape sh)
{
return Integer.valueOf(area%3).compareTo(sh.getValue()%3);
}
}


我得到的输出是

Bigger shape: 3
Bigger square: 2

经验教训:原始问题的答案是否定的。正如 Tagir Valeev 指出的那样,可以调用 findMaxSquare[]由于 Shape[] 的协变性质,没有赋值.

最佳答案

实际上您的代码无法编译。 Javac 1.7:

> "C:\Program Files\Java\jdk1.7.0_80\bin\javac.exe" GenericTest.java
GenericTest.java:32: error: method findMax in class GenericTest cannot be applied to given types;
Square maxSquare = findMax(new Square[]{new Square(1), new Square(2), new Square(3)});
^
required: S[]
found: Square[]
reason: inferred type does not conform to declared bound(s)
inferred: Square
bound(s): Comparable<Square>
where S is a type-variable:
S extends Comparable<S> declared in method <S>findMax(S[])
1 error

Javac 1.8:

>"C:\Program Files\Java\jdk1.8.0_40\bin\javac.exe" GenericTest.java
GenericTest.java:32: error: incompatible types: inference variable S has incompatible bounds
Square maxSquare = findMax(new Square[]{new Square(1), new Square(2), new Square(3)});
^
equality constraints: Shape
upper bounds: Square,Comparable<S>
where S is a type-variable:
S extends Comparable<S> declared in method <S>findMax(S[])
1 error

欧洲法院 3.10.2:

>java -jar org.eclipse.jdt.core_3.10.2.v20150120-1634.jar -source 1.7 GenericTest.java
----------
1. ERROR in C:\projects\Test\src\GenericTest.java (at line 32)
Square maxSquare = findMax(new Square[]{new Square(1), new Square(2), new Square(3)});
^^^^^^^
Bound mismatch: The generic method findMax(S[]) of type GenericTest is not applicable for the arguments (GenericTest.Square[]).
The inferred type GenericTest.Square is not a valid substitute for the bounded parameter <S extends Comparable<S>>
----------
1 problem (1 error)

所有编译器都按预期生成正确的错误消息。如果你声明 findMax方法为 public static <S extends Comparable<? super S>> S findMax(S[] arr) ,然后错误消息正确消失。

更新 发布完整代码后,问题就变得很清楚了。不同之处在于您不分配 findMax 的结果。到变量:

System.out.println("Bigger shape: "+findMax(new Shape[] {new Shape(1),new Shape(3)}));
System.out.println("Bigger square: "+findMax(new Square[] {new Square(3),new Square(2)}));

所以在这两种情况下 <S>被推断为 Shape作为Square[]类型 IS-A Shape[]类型。

关于java - 子类型 "inherit"通用接口(interface)是否使用该特定子类型 (Java) 进行参数化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32366552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com