Java泛型:多个泛型参数?
我想知道是否可以编写一个接受多种泛型类型的函数,如下所示:Java泛型:多个泛型参数?,java,generics,parameters,Java,Generics,Parameters,我想知道是否可以编写一个接受多种泛型类型的函数,如下所示: public int void myfunction(Set<T> a, Set<T> b) { return 5; } Set<Integer> setA = new HashSet<Integer>(); Set<String> setB = new HashSet<String>(); int result = myfunction(setA, se
public int void myfunction(Set<T> a, Set<T> b) {
return 5;
}
Set<Integer> setA = new HashSet<Integer>();
Set<String> setB = new HashSet<String>();
int result = myfunction(setA, setB);
public int void myfunction(集合a、集合b){
返回5;
}
Set setA=新的HashSet();
Set setB=新的HashSet();
int结果=myfunction(setA,setB);
这样行吗?每个参数中的泛型是否意味着每个参数必须具有相同的泛型类型T?a和b必须是相同类型的集合。但是没有什么能阻止你写作
myfunction(Set<X> a, Set<Y> b)
myfunction(集合a、集合b)
是-这是可能的(虽然不是通过方法签名),是的,通过签名,类型必须相同
使用您提供的签名,T
必须与呼叫站点上的单个类型(例如String
或
)关联。但是,您可以声明采用多个类型参数的方法签名
public <S, T> void func(Set<S> s, Set<T> t)
您可能想看看java.util
包中集合类的一些方法签名。泛型确实是一个相当复杂的主题,特别是在考虑通配符(?extends
和?super
)时。例如,通常情况下,可能将集
作为参数的方法也应该接受集
。在这种情况下,您会看到这样的签名:
public void baz(Set<? extends T> s);
public void baz(Set您可以在类型或方法上声明多个类型变量。例如,在方法上使用类型参数:
<P, Q> int f(Set<P>, Set<Q>) {
return 0;
}
intf(Set,Set){
返回0;
}
在函数定义中,您将集合a和集合b约束为同一类型。您还可以编写
public <X,Y> void myFunction(Set<X> s1, Set<Y> s2){...}
public void myFunction(集合s1,集合s2){…}
更重要的是,您可以继承泛型:)
@SuppressWarnings(“未选中”)
公共课(课堂){
return(T)somethingHolderMap.get(clazz);
}
您可以采用以下方法之一:
1) 基本、单一类型:
//One type
public static <T> void fill(List <T> list, T val) {
for(int i=0; i<list.size(); i++){
list.set(i, val);
}
}
除了您需要在签名中声明类型参数,就像在我的回答中一样,您不应该接受错误的回答@oxbow_lakes是第一个做对的人。我不明白为什么这两个问题都是“是”(具体来说,为什么“这行吗?”)的答案是“是”)。上面的函数(我知道是人为的,带有int返回)不会接受两种不同的类型,因为它要求两个参数使用相同的泛型类型t,对吗?对不起,我的意思是“是的,这是可能的”和“是的,在您定义的签名中,类型需要相同”
@SuppressWarnings("unchecked")
public <T extends Something<E>, E extends Enum<E> & SomethingAware> T getSomething(Class<T> clazz) {
return (T) somethingHolderMap.get(clazz);
}
//One type
public static <T> void fill(List <T> list, T val) {
for(int i=0; i<list.size(); i++){
list.set(i, val);
}
}
// multiple types as parameters
public static <T1, T2> String multipleTypeArgument(T1 val1, T2 val2) {
return val1+" "+val2;
}
//Raised compilation error
public static <T1, T2> T3 returnTypeGeneric(T1 val1, T2 val2) {
return 0;
}
public static <T1, T2, T3> T3 returnTypeGeneric(T1 val1, T2 val2) {
return 0;
}
package generics.basics;
import java.util.ArrayList;
import java.util.List;
public class GenericMethods {
/*
Declare the generic type parameter T in this method.
After the qualifiers public and static, you put <T> and
then followed it by return type, method name, and its parameters.
Observe : type of val is 'T' and not '<T>'
* */
//One type
public static <T> void fill(List <T> list, T val) {
for(int i=0; i<list.size(); i++){
list.set(i, val);
}
}
// multiple types as parameters
public static <T1, T2> String multipleTypeArgument(T1 val1, T2 val2) {
return val1+" "+val2;
}
/*// Q: To audience -> will this compile ?
*
* public static <T1, T2> T3 returnTypeGeneric(T1 val1, T2 val2) {
return 0;
}*/
public static <T1, T2, T3> T3 returnTypeGeneric(T1 val1, T2 val2) {
return null;
}
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
System.out.println(list.toString());
fill(list, 100);
System.out.println(list.toString());
List<String> Strlist = new ArrayList<>();
Strlist.add("Chirag");
Strlist.add("Nayak");
System.out.println(Strlist.toString());
fill(Strlist, "GOOD BOY");
System.out.println(Strlist.toString());
System.out.println(multipleTypeArgument("Chirag", 100));
System.out.println(multipleTypeArgument(100,"Nayak"));
}
}
[10, 20]
[100, 100]
[Chirag, Nayak]
[GOOD BOY, GOOD BOY]
Chirag 100
100 Nayak