简体   繁体   English

我的方法签名中这些东西叫什么? 泛型?

[英]What are these things in my method signature called? Generics?

I have a method signature that sorts a given map by values. 我有一个方法签名,可以按值对给定的映射进行排序。

public static <K extends Comparable<K>, V extends Comparable<V>> Map<K,V> sortByValues(Map<K,V> map){

I am trying to understand the terminology for the things in this method signature. 我试图了解此方法签名中事物的术语。 I get the public , static , Map<K,V> , the function name and the parameter part. 我得到publicstaticMap<K,V> ,函数名称和参数部分。

My confusion is on the <K extends Comparable<K>, V extends Comparable<V>> part. 我的困惑是在<K extends Comparable<K>, V extends Comparable<V>>部分。 It's purpose is to define what K and V are in the context of the method, correct? 目的是定义方法上下文中的KV ,对吗? What is the proper term to describe this? 用什么恰当的术语来形容呢?

Any recommended readings or articles on Generics? 任何有关泛型的推荐读物或文章?

Yes, K and V here are generic type parameters . 是的,这里的KV是通用类型参数 To be more specific, as @aruisdante points out in comments, they are bounded type parameters because they specify a bound that the parameterized types must satisfy - namely that they both must implement Comparable. 更具体地说,正如@aruisdante在注释中指出的那样,它们是有界类型参数,因为它们指定了参数化类型必须满足的界限-即它们都必须实现Comparable。

It seems that your question is about the parameter in general though, not specifically about bounded parameters. 似乎您的问题是关于参数的,而不是关于有界参数的。 You should definitely do some reading on your own, as you're asking about a big (and important) topic , but I'll take a stab at introducing it: 当您问到一个重要的(且很重要的)主题时 ,您绝对应该自己做一些阅读,但是在介绍这个主题时 ,我会稍作努力:

You might be familiar with seeing type parameters in a different context - Map<String, Integer> : <String, Integer> here are type parameters as well, and they specify that the keys in this map are String s and the values in it are Integer s. 您可能对在不同上下文中看到类型参数很熟悉-Map Map<String, Integer><String, Integer>此处也是类型参数,它们指定此映射中的键为String并且其中的值为Integer s。 In the context of a method declaration, type parameters specify what the return type will be. 在方法声明的上下文中,类型参数指定返回类型是什么。 This means you can write generic methods that return different types each time they're called, depending on what you pass in: 这意味着您可以编写泛型方法 ,这些泛型方法每次调用时都会返回不同的类型,具体取决于您传入的内容:

Note that the K and V are the type parameters on both the method itself and on the parameter passed in: This tells Java to determine K and V by looking at the key and value types of the map passed in, returning a map with the same types. 请注意, KV都是方法本身和传入的参数上的类型参数:这告诉Java通过查看传入的映射的键和值类型来确定KV ,并返回具有相同值的映射类型。 If you call sortByValue(fooMap) and fooMap is a Map<String, FooType> , it's going to return a Map <String, FooType> , and if fooMap is a Map<Integer, BarType> , you'll get a Map<Integer, BarType> back. 如果调用sortByValue(fooMap)并且fooMapMap<String, FooType> ,它将返回Map <String, FooType> ;如果fooMapMap<Integer, BarType>fooMap获得Map<Integer, BarType>返回。

For a clearer example, consider: 对于更清晰的示例,请考虑:

public <T> foobar(T t) {  }

Here the type parameter is unbounded - meaning that T can be any class, so you can pass an object of any class in as a parameter and get an object of the same type returned. 这里的type参数是无界的-意味着T可以是任何类,因此您可以将任何类的对象作为参数传递,并获得返回相同类型的对象。 Or 要么

public<T> convertTo(Object o, Class<T> clazz) { } 

Here, you can pass in an object of any class as the first parameter, and as the second parameter a Class object that determines what T will be. 在这里,您可以传入任何类的对象作为第一个参数,而作为第二个参数传入确定TClass对象。 So you can do: 因此,您可以执行以下操作:

Fooclass s = convertTo(someObject, Fooclass.class) 

which, as you can probably imagine, can be a very useful pattern. 您可能会想到,这可能是非常有用的模式。

They are examples of Parameterized Types according to the JLS Chapter 4. Types, Values, and Variables which says in part, 根据JLS第4章,它们是参数化类型的示例。 类型,值和变量部分说明:

A generic class or interface declaration C ( §8.1.2 , §9.1.2 ) with one or more type parameters A1,...,An which have corresponding bounds B1,...,Bn defines a set of parameterized types, one for each possible invocation of the type parameter section. 具有一个或多个类型参数A1,...,An且具有对应范围B1,...,Bn的泛型类或接口声明C(第8.1.2节 ,第9.1.2节 )定义了一组参数化类型,一个对于类型参数部分的每个可能的调用。

Each parameterized type in the set is of the form C where each type argument Ti ranges over all types that are subtypes of all types listed in the corresponding bound. 集合中的每个参数化类型都具有C的形式,其中每个类型参数Ti覆盖所有类型,这些类型是在相应边界中列出的所有类型的子类型。 That is, for each bound type Si in Bi, Ti is a subtype of Si[F1:=T1,...,Fn:=Tn]. 即,对于Bi中的每个结合类型Si,Ti是Si [F1:= T1,...,Fn:= Tn]的子类型。

... ...

Given a type declaration specifier immediately followed by a type argument list, let C be the final Identifier in the specifier. 给定类型声明说明符,紧随其后的是类型参数列表,令C为说明符中的最终标识符。

The way I see generics is the same way I see classes, only for types and methods. 我看到泛型的方式与我看到类的方式相同,仅适用于类型和方法。 They generalize methods and data structures to natively support all bounded types. 它们概括了方法和数据结构以本地支持所有有界类型。

Instead of coding the same object with the same methods over and over again it says "Okay guys, whatever class you'll tell me in the < > , I'll understand". 而不是一遍又一遍地用相同的方法编码相同的对象,而是说“好吧,不管您在< >告诉我什么类,我都会理解的”。

Suppose you want to write a binary search in an array of unknown object... you can write the following signature: 假设您要在未知对象数组中编写二进制搜索...,您可以编写以下签名:

public boolean binarySearch(Object[] array)...

But what if you wanted the method to be accessed by only certain types of data (types that are compareable to each other)? 但是,如果您希望仅某些类型的数据(可相互比较的类型)可以访问该方法,该怎么办? this is how you would accomplish that: 这是您将如何实现的方法:

public <T extends Comparable<T>> boolean binarySearch(T[] array)...

Which is read like: "This is a public method that receives only arrays which contain objects (we'll call them T) which implement the Comparable interface to the same classes (T). 如下所示:“这是一个公共方法,它仅接收包含对象的数组(我们将它们称为T),这些对象将Comparable接口实现为相同的类(T)。

Not only classes can be extended in generics, but interfaces as well (just like the example above). 不仅可以在泛型中扩展类,而且还可以扩展接口(就像上面的示例一样)。

For data structures like 对于像这样的数据结构

Vector<Integer> v;

It means the Vector v will only hold Integer objects and not other types. 这意味着Vector v将仅保存Integer对象,而不包含其他类型。

Wildcards have the same principle in bounding the object for example: 通配符在限制对象方面具有相同的原理,例如:

Vector<? extends Person> v;

That vector v is going to hold only classes whom super-class is Person. 该向量v将仅容纳超类为Person的类。

This 这个

Vector<?> v;

is roughly equivalent to this 大致相当于

Vector<Object> v;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM