如何在语法上正确实现类型为,V>?

时间:2022-09-02 09:16:41

如何在语法上正确实现类型为,V>?

K and V are guarantee to be either String or Integer. I've use generics quite a few times, but never <K extends Comparable<K>, V>, so I am struggling right now.

K和V保证为String或Integer。我使用泛型很多次,但从来没有 ,V>,所以我现在正在努力。 扩展comparable>

1 个解决方案

#1


1  

When you implement a generic interface, you should always specify the type arguments. You can get away with not specifying them, but then you are using raw types, which you shouldn't.

实现通用接口时,应始终指定类型参数。你可以不指定它们,但是你使用原始类型,你不应该。

When you do specify them, you also need to specify them in a valid way.

当您指定它们时,还需要以有效的方式指定它们。

First option is wrong because of two reasons:

第一种选择是错误的,原因有两个:

  • When you specify the type arguments, you never rewrite the bounds on that argument, so writing K extends Comparable<K> is wrong
  • 当你指定类型参数时,你永远不会重写该参数的边界,所以写K扩展Comparable 是错误的

  • K and V are not defined anywhere.
  • K和V没有在任何地方定义。

Second option is wrong because you rewrote the bounds on K. This option differs from the first option in that K and V are defined here - K and V are the type arguments of Son.

第二个选项是错误的,因为你重写了K上的边界。这个选项与第一个选项的不同之处在于K和V在这里定义--K和V是Son的类型参数。

Third option compiles, but it uses raw types, which kind of defeats the purpose of using generics in the first place.

第三个选项编译,但它使用原始类型,这种方式首先打败了使用泛型的目的。

Ideally, you would write something like:

理想情况下,你会写如下:

class Son<K extends Comparable<K>, V> implements Father<K, V> {

}

Note how I did not rewrite the bounds, and defined K and V as the type arguments for Son.

注意我没有重写边界,并将K和V定义为Son的类型参数。

Alternatively, specify a concrete type:

或者,指定具体类型:

class Son implements Father<String, Integer> {

}

#1


1  

When you implement a generic interface, you should always specify the type arguments. You can get away with not specifying them, but then you are using raw types, which you shouldn't.

实现通用接口时,应始终指定类型参数。你可以不指定它们,但是你使用原始类型,你不应该。

When you do specify them, you also need to specify them in a valid way.

当您指定它们时,还需要以有效的方式指定它们。

First option is wrong because of two reasons:

第一种选择是错误的,原因有两个:

  • When you specify the type arguments, you never rewrite the bounds on that argument, so writing K extends Comparable<K> is wrong
  • 当你指定类型参数时,你永远不会重写该参数的边界,所以写K扩展Comparable 是错误的

  • K and V are not defined anywhere.
  • K和V没有在任何地方定义。

Second option is wrong because you rewrote the bounds on K. This option differs from the first option in that K and V are defined here - K and V are the type arguments of Son.

第二个选项是错误的,因为你重写了K上的边界。这个选项与第一个选项的不同之处在于K和V在这里定义--K和V是Son的类型参数。

Third option compiles, but it uses raw types, which kind of defeats the purpose of using generics in the first place.

第三个选项编译,但它使用原始类型,这种方式首先打败了使用泛型的目的。

Ideally, you would write something like:

理想情况下,你会写如下:

class Son<K extends Comparable<K>, V> implements Father<K, V> {

}

Note how I did not rewrite the bounds, and defined K and V as the type arguments for Son.

注意我没有重写边界,并将K和V定义为Son的类型参数。

Alternatively, specify a concrete type:

或者,指定具体类型:

class Son implements Father<String, Integer> {

}