Java名称冲突,有相同的擦除,既不隐藏其他 [英] Java name clash, have the same erasure, neither hides the other

查看:193
本文介绍了Java名称冲突,有相同的擦除,既不隐藏其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个名字冲突错误,我不知道我应该如何解决这个问题。
我有两个类,我正在使用重载方法createSensors。在这里简单的是产生这个问题的代码:

  public abstract class ClassA {
public static List< Sensor> createSensors(Collection< ;? extends ClassA> list){
List< Sensor> sensors = new ArrayList< Sensor>();
for(ClassA s:list){
sensors.add(s.getSensor());
}
返回传感器;



public abstract class ClassB extends ClassA {
public static List< Sensor> createSensors(Collection< ;? extends ClassB> list){
List< Sensor> sensors = new ArrayList< Sensor>();
for(ClassB s:list){
sensors.add(s.getSensor());
}
返回传感器;
}
}


解决方案

一般的解决方案是使用不同的名称。这些方法可以在没有继承关系的类中,因为它们不是实例方法。



正如指出的那样,问题中的方法实现是相同的(排除错字)。

(这个重载问题经常与删除运行时类型相混淆。重载是一个链接时间而不是动态问题,因此可以用语言轻松修复。这不是一个特别有用的改变,鼓励重载并不是一个好主意。)


I am getting this name clash error and i don't know how should i solve the problem. I have two classes and i am using overloaded method "createSensors". To simplify here is the code that generates the problem:

public abstract class ClassA {
    public static List<Sensor> createSensors(Collection<? extends ClassA> list) {
        List<Sensor> sensors = new ArrayList<Sensor>();
        for (ClassA s : list) {
           sensors.add(s.getSensor());
        }
        return sensors;
    }
}

public abstract class ClassB extends ClassA {
    public static List<Sensor> createSensors(Collection<? extends ClassB> list) {
        List<Sensor> sensors = new ArrayList<Sensor>();
        for (ClassB s : list) {
           sensors.add(s.getSensor());
        }
        return sensors;
   }
}

解决方案

The general solution is to use different names. These methods could be in classes without an inheritance relationship as these are not instance methods.

As pointed out, the method implementation in the question are the same (typo excepted).

(This issue with overloading is often confused with erasure of runtime types. Overloading is a link-time rather than a dynamic issue, so could be easily fixed in the language. It's just not a particularly useful change, and not a good idea to encourage overloading.)

这篇关于Java名称冲突,有相同的擦除,既不隐藏其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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