我对ArrayLists和泛型有些困惑,例如,在下面的程序中,当我声明Gen<Integer>iOb=new Gen<Integer>(88);时,它声明了一个通用类型的Integer,对吗?但是,如果我以同样的方式宣布一个数组列表的话?对于数组,它是在尖括号中的类型,然而,在研究泛型时,它说尖括号中的类型是泛型类型?我如何知道它是类类型的数组列表还是泛型的数组?
//A simple generic class
//Here, T is a type parameter that
///will be replaced by a real type
//when an object of type gen is created
class Gen<T> {
T ob;//declare an object of type T
//Pass the constructor a refernce to
//an object of type T
Gen(T o) {
ob = o;
}
//return ob
T getob() {
return ob;
}
//show type of t
void showType() {
System.out.println("Type of T is " + ob.getClass().getName());
}
}
class GenDemo {
public static void main(String[] args) {
//create a gen reference for integers
Gen<Integer> iOb;
//Create a Gen<Integer>Object and assign its
//reference to iob, notice the use of autoboxing
//to encapsulate the value 88 within an integer object
iOb = new Gen<Integer>(88);
//show the type off data used by iob
iOb.showType();
//get the value in Iob notice that no cast is needed
int v = iOb.getob();
System.out.println("Value: " + v);
System.out.println();
//create a gen object for strings
Gen<String> strOb = new Gen<String>("Generic Test");
//show the type of data
strOb.showType();
//get the value of strOb, again notice that no cast is needed
String str = strOb.getob();
System.out.println("Value :" + str);
}
}发布于 2016-06-28 20:14:01
当我声明时,
Gen<Integer> iOb = new Gen(88);它声明了一种通用类型的整数正确吗?
这是错误的。您正在声明/创建一个Gen对象,该对象包含一个T extends Object。在Gen代码中,只能使用允许在Object (如getClass() )上使用的函数。
Java记住,iOb将引用必须包含Integer对象的Gen对象,并将对getob()的返回值生成自动类型转换.
int v=iOb.getob();编译器将其解释为编写了以下内容:
int v = (Integer) iObj.getob();其中getob()被认为只是返回一个Object。请注意,这是一个编译时转换,以方便程序员。
但是,如果我以同样的方式宣布一个数组列表的话?对于数组,它是在尖括号中的类型,然而,在研究泛型时,它说尖括号中的类型是泛型类型?我如何知道它是类类型的数组列表还是泛型的数组?
class SomeClass<T> { ... }声明了一个泛型类型。尖括号之间的部分是泛型类的类型参数。
当泛型类用作变量的类型时,它被认为是泛型类的专门化,类型参数可以是具体的类、接口或其他泛型类。
发布于 2016-06-28 20:14:46
您似乎混淆了泛型和类型参数的概念。
鉴于你的班级:
class Gen<T> {
// ...
}Gen是一个泛型类(一种特定的泛型类型),因为它被声明为至少有一个类型参数,在本例中是T。实例化Gen时,有时在其他情况下,提供绑定到T的类型表达式。
Gen<?> gen = new Gen<String>();给定的类型表达式(?和String)通常也称为“类型参数”,就像类Gen定义中的T一样。特定的类型参数可以由泛型类(例如:Gen<List<String>>中的List<String> )形成,但“泛型类”不是“类型参数”的同义词。
ArrayList也是一样。它是一个具有一个类型参数的泛型类。特定声明提供类型表达式作为这些类型参数的值:
List<Integer> list = new ArrayList<Integer>();在那里,List<E>和ArrayList<E>是泛型类型;后者是具体的泛型类。两边的Integer是应用于list声明和ArrayList实例化的类型参数;它在这个上下文中或任何其他类中都不被称为“泛型类”。
发布于 2016-06-28 20:15:29
你的问题措辞有点奇怪,但我试试看。泛型用于类型安全,特别是用于集合。如果您要像这样声明一个ArrayList:
ArrayList a= new ArrayList(); 然后,您可以将任何类型的对象放入到ArrayList中。这是非常危险的,因为当您从列表中检索对象时,您需要知道要从列表中检索什么样的对象。你不会想把一个猫对象拉到一个人的引用中!猫不是人!这就是仿制药进来的地方。这样声明您的ArrayList:
ArrayList<People> p= new ArrayList<People>(); 允许您具有类型安全性。不要再麻烦那些讨厌的猫对象进入你的人阵列!
至于这一点:
//get the value in Iob notice that no cast is needed
int v = iOb.getob();你好像被取消拳击搞糊涂了。这与泛型无关。我建议您查找自动装箱和取消装箱。
最后:
//get the value of strOb, again notice that no cast is needed
String str = strOb.getob();我看不出你在这里不懂什么。字符串是对象。你在里面放了一个字符串对象,然后把一个字符串对象拉出来。
我还建议创建一个简单的java对象,比如person,这样您就可以在Gen类中创建一个泛型,比如字符串,并尝试将Person对象放入其中。看看它带来了什么错误。
https://stackoverflow.com/questions/38085114
复制相似问题