今天怎么老是有乱码啊~~ import java.util.*;public class ArrayIsNotIterable { static <T> void test(Iterable<T> ib) { for(T t : ib) System.out.print(t + " "); } public static void main(String[] args) { test(Arrays.asList(1, 2, 3)); String[] strings = { "A", "B", "C" }; // An array works in foreach, but it's not Iterable: //! test(strings); // You must explicitly convert it to an Iterable: test(Arrays.asList(strings)); } }这里有2个<T>,我还没学泛型,所以提前想问下,是否是如果要设置形参中的参数类型,那么方法的返回值前也要写一个<T>?还有<T>里的T的意思是指所有接受这个接口的类型?
问题2: public class Test1 extends Test2<Pet>{ } 这里 Test2<Pet>; 意思是说,使用所有Test2这个类里的方法,其参数必须传Pet类型的?
import java.util.*;public class ArrayIsNotIterable {
static <T> void test(Iterable<T> ib) {
for(T t : ib)
System.out.print(t + " ");
}
public static void main(String[] args) {
test(Arrays.asList(1, 2, 3));
String[] strings = { "A", "B", "C" };
// An array works in foreach, but it's not Iterable:
//! test(strings);
// You must explicitly convert it to an Iterable:
test(Arrays.asList(strings));
}
}这里有2个<T>,我还没学泛型,所以提前想问下,是否是如果要设置形参中的参数类型,那么方法的返回值前也要写一个<T>?还有<T>里的T的意思是指所有接受这个接口的类型?
public class Test1 extends Test2<Pet>{ } 这里 Test2<Pet>; 意思是说,使用所有Test2这个类里的方法,其参数必须传Pet类型的?