为什么用Java编译?

时间:2013-06-24 00:00:31

标签: java generics

为什么要编译?

B在A中使用,没有任何通用参数,这是用Java编译的。这是怎么回事?

interface B<T>
{
    public T Foo(T value);
}

public class A
{
    public B What()
    {
        return null;
    }

    public void Foo()
    {
        B x = What();
        x.Foo(123);
    }
}

2 个答案:

答案 0 :(得分:8)

这是为了与J2SE 5.0之前的Java兼容。你应该得到一个rawtypes警告(注意编译器警告)。

答案 1 :(得分:7)

您只是在这里使用raw类型的B。就像

 List list = new ArrayList(); // defined as: public interface List<E>

完美,有效;不推荐。