Generics definition

♀尐吖头ヾ 提交于 2019-12-14 02:33:57

问题


I can't grasp the whole generics declaration, even after reading countless articles and entries in books about Java, none of them seem to explain it in plain simple and clean way. Please can anyone explain me those?:

class Something<T> {...}

I know what T is, and I understand that we use generics when we want to write a general definition/method for any type of type Object we pass in, instead having different types of methods specific for one type of Object extension, we write one generic one that can include one or more types (hence the upper case T). But one thing that buggs me the most is the next declaration example:

public <T extends Comparable<T>> void Something{...}

First off; Comparable is an interface not a class (correct me if I'm wrong) so 1st why is it extends instead of implements. Now, why does it HAVE to be declared (this whole < T ...> thing) BEFORE void and now Comparable<T> ?? What does that mean? That the Object type that gets in has to have implements Comparable<T>? So if I want to pass as type parameter the class class Something{...} i would get an error, but passing class Something implements Comparable<T>{...} would be fine? Please demystify this to me :( I have an exam on this tomorrow and I can't move to other things without grasping this one... :(


回答1:


First of all in Java you have two things:

  • generic types like class GenericContainer<T>
  • generic methods like your example public <T> method()

First are used to define classes which are generic, while latter are used to define a method which is generic over a type. These are two different things although both use the same principles.

The fact that you have to state explicitly that a method is generic like in

public <T extends Comparable<T>> void myGenericMethod(...)

is just the way you declare a generic method in Java. It's just syntax. If you want to declare a generic method then you have to specify the type parameters with that syntax before the method signature.

Regarding your questions about T extends Comparable<T> think about this fact: when using a generic variable, like T myVariable, you will have to use this variable for some purpose. Since Java is a strong typed language everything you will be able to know about T must be decided at compile type.

In your example suppose that you want to be able to sort two objects, now

public <T> void method(T v1, T v2)

will work but you won't be able to call anything on v1 or v2, you wouldn't be allowed because T is just a generic type with no constraints. v1.compareTo(v2) leads to a compile error. So, how do you fix this problem?

You enforce constraints over type variables:

public <T extends Comparable<T>> void method(T v1, T v2)

In this way you will be forced to pass to method() just instances of the same class that must implement Comparable<T> or you will get a compile time error. But you will have the advantage that you will be allowed to call compareTo(..) because you are guaranteed that the objects you are passing to the method will be able to respond to it.




回答2:


In order:

  1. X extends Y and X super Y in generic type declarations are used to mean "X is-a Y" and "Y is-an X", respectively. The java developers did not wish to add a new keyword meaning "extends or implements".
  2. The declaration precedes the return type, presumably because once you have declared your generic type variables, the return type might use some of them. Incidentally, as @Jack points out, the < T extends Comparable< T > > in this case is a type declaration for a generic method, not a generic class. Instance methods inside a generic class can refer at will to the class' declared generic types without re-declaring them; in fact if you do re-declare them you will be shadowing them.
  3. The "extends Comparable< T >" is a constraint. You can only instantiate your class Something with a type that is known to be self-comparable. So Something< Integer > si = new Something<>(); is legal, but Something< Object > so = new Something<>(); is not, as Object does not implement Comparable< Object >.


来源:https://stackoverflow.com/questions/19848961/generics-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!