There is Java API: Morphia
class Morphia defines method like:
<T> T fromDBObject(Class<T> entityClass, com.mongodb.DBObject dbObject)
Can some o开发者_JS百科ne explain what <T> T and Class<T>
means? Is the method returning a class of type T in a collection?
Another API on Inteface DataStore:
<T,V> Query<T> find(Class<T> clazz, String property, V value)
What <T,V> Query<T> means? Is the method returning an object of type Query that is then surrounded by collection `<T,V>` and `<T>`. This part is very confusing.
Is it correct to say whenever angle bracket (< > )
is involved, it always means a Java collection is involved?
Sorry I forgot to mark some content in this question as code otherwise SO was escaping changing entire meaning of the question and hence the 2 answers by @Vash and @fiver are in accordance to the question before this edit.
Thanks for pointing to some tutorials out there but please provide specifc answer from your expertise in Java generics to the question which will then help me to understand the tutorials better.
Generics are not limited to collections but collections are good examples to understand why you need generics and how you can use them.
Assume that you create a new List
object and you know it will only contain specific type of objects (e.g., MyClass
objects). If you could somehow specify it in your code and it can be checked automatically then you can make sure that no other type of object will be inserted to this list. Moreover when other developers read your code, they easily understand that this list contains only `MyClass' objects.
Generics let us to include such information in our code without rewriting the List
class. In fact, List
functionality is independent from the type of objects it includes. So to create your own List
you write this code:
List<MyClass> myList = new List<MyClass>();
Now you can imagine cases where more than one generic type can be specified. A good example of this case is Map
class. You can define the types of keys and values. So you see <K,V>
in the Map
class declaration (Map<K,V>
).
In addition to Classes, generics can also be used in method/constructor declaration. In your example:
<T,V> Query<T> find(Class<T> clazz, String property, V value)
There is a method which has two generic types T and V and you can specify them (or they can be inferred by compiler) when you call the method. Here, <T,V>
in the declaration explicitly indicates this method feature.
Whenever you see < >, this means generics class - not just collection.
Class is just an example of a generic class: http://download.oracle.com/javase/6/docs/api/java/lang/Class.html
T in the declaration means object of any type (T is the type).
So the DBFromObject method is a generic method, where the generic type is denoted with T, which returns an object of this generic type T and accepts a parameter of type Class<T>
.
The Query
and Class are Java class.
The <T>
mean that some generic parameter T will be used.
Please read the tutorial, this will clarify you the generics.
public <T> T fromDBObject(Class<T> entityClass, com.mongodb.DBObject dbObject)
is a good example of using Class Literals as Runtime-Type Tokens. The declaration is very similar to the example near the bottom of the page. One reading might be, "Given a class literal of type T named entityClass
and a dbObject
, the method fromDBObject()
returns an object of type T. In this case, the symbol T stands for the type that the Class
object represents.
精彩评论