在博客堂看到了一篇关于C#2.0范性的小测验(http://blog.joycode.com/ninputer/archive/2005/11/02/66256.aspx),考虑到JDK 5.0也引进了范性,特意在JDK 5.0做了相应的测试。
Quiz1:判断以下代码是否有错,如果有错,说出错误类型,如果无错,说出输出结果。
public class GenericQuiz1{
public static void main(String[] args) { A a = new A(); a.internalCall(2005); }}
class A{ void internalCall(T p) { test(p); } void test(T p) { System.out.println("use T"); } void test(Integer p) { System.out.println("use Integer"); }}
Quiz2:判断以下代码是否有错,如果有错,说出错误类型,如果无错,说出输出结果。public class GenericQuiz1{
public static void main(String[] args) { A a = new A(); a.test(2005); }}
class A{ void test(T p) { System.out.println("use T"); }
void test(int p) { System.out.println("use int"); }}
Quiz3:判断以下代码是否有错,如果有错,说出错误类型,如果无错,说出输出结果。public class GenericQuiz1{
public static void main(String[] args) { A a = new A(); a.test(2005); }}
class A{ void test(T p) { System.out.println("use T"); }
void test(Integer p) { System.out.println("use Integer"); }}
Quiz4:判断以下代码是否有错,如果有错,说出错误类型,如果无错,说出输出结果。public class GenericQuiz1{
public static void main(String[] args) { A a = new A(); a.test(2005); }}
class A{ void test(T p) { System.out.println("use T"); }
void test(U p) { System.out.println("use U"); }}