C++中有,template<class T>
Java中怎么实现的,我看到有Interface Comparator<T>算是模板吗?
Java中怎么实现的,我看到有Interface Comparator<T>算是模板吗?
解决方案 »
- 原创!阿拉伯数字换算成汉字的表达形式!
- 请教一个简单的关于JAVA包的问题
- 各位帮我看看 如何用 JFileChooser 打开一张图片到 ImagePanel。
- 泡MM必杀技 如何获得给定日期(2006/06/10)后100天的日期值, 如何获得? Java 或 SQL 实现都成
- DataInputStream的问题,帮帮忙哈1
- volatile 指令重排序问题
- 运行时出错了,为什么?请帮我调试一下!
- 在java里如何实现二进制、十六进制、十进制之间的转换?
- 这样写有什么好处?
- 问题求助
- 怎样取出一个字符串中的数字?
- 原来一个servlet文件是abc.class,打包成myjar.jar文件后不能运行了?
但是本质上是不一样的.
Java 2, v5.0 (Tiger) New Features
by Herbert Schildt
ISBN: 0072258543
Publisher: McGraw-Hill/Osborne, 2004
*/// 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 reference 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());
}
}
//Demonstrate the generic class.
public 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 of data used by iOb.
iOb.showType();
// Get the value of 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>("Generics Test");
// Show the type of data used by strOb.
strOb.showType();
// Get the value of strOb. Again, notice
// that no cast is needed.
String str = strOb.getob();
System.out.println("value: " + str);
}
}