在Java 7中编译时出现名称冲突编译错误,但在Java 5中可以正常工作 [英] Name Clash compile error when compiled in java 7 but works fine in java 5

查看:88
本文介绍了在Java 7中编译时出现名称冲突编译错误,但在Java 5中可以正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public interface Expression {

}

public interface ArithmeticExpression extends Expression {

}


public class StaticMethodDemo {
  public static void print(Expression e) {
    System.out.println("StaticMethodDemo");
  }

  public static List<Expression> convert(
        Collection<? extends Expression> input) {
    return null;
 }
}


public class StaticMethodChild extends StaticMethodDemo {

    public static void print(ArithmeticExpression e) {
    System.out.println("StaticMethodChild");
   }

   public static List<ArithmeticExpression> convert(
        Collection<? extends ArithmeticExpression> input) {
    return null;
  }
}

以上代码在Java 5中编译,但在Java 7中不编译为什么?在Java 7中,它给出了名称冲突:StaticMethodChild类型的convert(Collection)方法具有与StaticMethodDemo类型的convert(Collection)相同的擦除,但不会隐藏它

Above code compiles in java 5 but not in java 7 why? In java 7 it gives "Name clash: The method convert(Collection) of type StaticMethodChild has the same erasure as convert(Collection) of type StaticMethodDemo but does not hide it"

推荐答案

除了stonedsquirrel的答案,即使方法不是静态的,您也会遇到相同的错误。

Besides stonedsquirrel's answer, even if the methods were not static, you would get the same error.

这是因为类型删除,您将尝试使用不兼容的类型覆盖convert。

This is because of Type Erasure, you would be trying to override convert with an incompatible type.

检查以下以得到很好的解释。

Check the following answer for a nice explanation.

这篇关于在Java 7中编译时出现名称冲突编译错误,但在Java 5中可以正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆