package test;
public class genericusage {
/**
* 测试范型编程。
*
* @param <t>
* @param e
* @return
* @throws instantiationexception
* @throws illegalaccessexception
*/
public <t> t getobject(class<? extends t> e) throws instantiationexception,
illegalaccessexception {
return e.newinstance();
}
}
package test;
import junit.framework.testcase;
public class genericusagetest extends testcase {
genericusage gu = new genericusage();
public void testgetobject() throws instantiationexception,
illegalaccessexception {
string str = gu.getobject(string.class);
assertnotnull(str);
}
}
例子中的方法,返回值是在运行时传入的。
调用者不需要再作转型了。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器