黑马程序员技术交流社区

标题: 静态方法为什么不管类中定义的泛型,如果静态方法操作... [打印本页]

作者: 靓仔    时间: 2014-2-20 18:11
标题: 静态方法为什么不管类中定义的泛型,如果静态方法操作...
本帖最后由 靓仔 于 2014-2-21 08:17 编辑

静态方法为什么不管类中定义的泛型,如果静态方法操作应用数据不确定除了定义在方法上还可以定义在哪儿?
class Test<T>
{
        public void show(T t)
        {
                System.out.println(t);
        }
        public  void print(T t)
        {
                System.out.println(t);
        }
}
*/
class Demo<T>
{
        public<T> void show(T t)
        {
                System.out.println("show:"+t);
        }
        public<Q> void print(Q q)
        {
                System.out.println("print:"+q);
        }
        public static<W> void method(W t)
        {
                System.out.println("method:"+t);
        }
}
class GenericDemo4
{
        public static void main(String[] args)
        {
                /*
                Test<String> t=new Test<String>();

                        t.show("haha");
                Test<Integer> t1=new Test<Integer>();
                        t1.print(4);
                */
                Demo<String> d=new Demo<String>();
                d.show("hah");
                d.show(new Integer(4));
                d.print("heihei");
                Demo.method("hahahhah");
        }
}

作者: 一年_Hei    时间: 2014-2-20 19:04
在类上加泛型,是作用整个类的成员。但不包括静态成员。因为泛型是作用在编译时。运行时直接过滤掉。静态方法随类加载而加载。可以通过类名直接调用。类上的泛型是作用在对象上。所以类上的泛型不能作用在静态上




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2