阳光VIP

少壮不努力,老大徒伤悲。平日弗用功,自到临期悔。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

JDK5.0 范型小测验

Posted on 2012-02-03 10:28  阳光VIP  阅读(167)  评论(0编辑  收藏  举报

在博客堂看到了一篇关于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");
    }
}