简体   繁体   English

为什么用Java编译?

[英]Why does this compile in Java?

Why does this compile? 为什么编译?

B is used in A without any generic parameters, and this compiled in Java. B在A中使用,没有任何通用参数,这是用Java编译的。 What is going on here? 这里发生了什么?

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);
    }
}

This is for compatibility with pre-J2SE 5.0 Java. 这是为了与J2SE 5.0之前的Java兼容。 You should get a rawtypes warning (take notice of the compiler warnings). 你应该得到一个rawtypes警告(注意编译器警告)。

You're just using a raw type of B here. 你只是在这里使用raw类型的B Just like 就像

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

Perfectly, valid; 完美,有效; not recommended though. 不推荐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM