JDK5.0 范型小测验1

王朝java/jsp·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

在博客堂看到了一篇关于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<Integer> a = new A<Integer>();

a.internalCall(2005);

}

}

class A<T>

{

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<Integer> a = new A<Integer>();

a.test(2005);

}

}

class A<T>

{

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<Integer> a = new A<Integer>();

a.test(2005);

}

}

class A<T>

{

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<Integer> a = new A<Integer>();

a.test(2005);

}

}

class A<T>

{

void test(T p)

{

System.out.println("use T");

}

<U> void test(U p)

{

System.out.println("use U");

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航