java5 泛式中的 T是什么意思?
java5 泛式中的 T是什么意思?
public class classA<T>{}
和
public class classA{}
有什么区别?
[解决办法]
楼主可以看看这篇文章,写的挺详细的:
http://liutiegang2.itpub.net/post/24466/228748
[解决办法]
template吧
[解决办法]
2楼说的对,就这么理解就可以了
[解决办法]
这个是java5.0的新特性:定义泛型类.
作用有点像(2楼所说)C++的模板template.
个人觉得"泛型"在集合类Collection里有作用,对定义泛型类(就像lz所提),泛型方法没有多大的意义
[解决办法]
接上:
public class classA <T>{}
和
public class classA{}
有什么区别?是:
1>public class classA{} 里的对象类变量,或返回对象类型的方法,这些属性可以"随意定".如
public class classA{
Object o1; --Object类型类变量
Animal a1; --Animal类型类变量
Cat c1; --Cat类型类变量
Integer fun(){} --返回Integer对象类型的方法
}
1>public class classA <T>{} 里如果有如上的类变量和方法,则这些的属性必须是T类型.
public class classA<Integer>{
Integer o1; --Integer类型类变量
Integer a1; --Integer类型类变量
Integer c1; --Integer类型类变量
Integer fun(){} --返回Integer对象类型的方法
}
[解决办法]
就是泛型参数哦,不用 T 用其他字母也行。用 T 只是习惯性,表示 Type。
也可以看到在泛型集合类中也有用到 E、K、V 什么的,分别表示 Element, Key, Value 用的
字母只是一个代号,并不是一定得用这个字母的 :)
[解决办法]