如何解决这种类型擦除?

时间:2015-02-11 14:49:06

标签: java

我想知道如何解决我在JDK 1.7中遇到的编译失败问题,该代码与JDK 1.6完美配合。

我得到的错误: MatchStrings.java:[71,36]错误:名称冲突:MatchStrings中的(ElementMatcher)和MatchElements中的(ElementMatcher)具有相同的擦除,但都没有隐藏其他

我的问题课程

public class MatchStrings extends MatchElements
{


public static class StringMatcherImpl extends ElementMatcherImpl<String> 
{
    public StringMatcherImpl(ElementMatcher<String> matcher)
    {
        super(matcher);
    }
}

public static class PrefixMatcher implements ElementMatcher<String>
{

    private final String _prefix;

    public PrefixMatcher(String prefix)
    {
        _prefix = prefix;
    }

    @Override
    public boolean matches(String str)
    {
        return str.startsWith(_prefix);
    }
}

public static class SuffixMatcher implements ElementMatcher<String>
{

    private final String _suffix;

    public SuffixMatcher(String suffix)
    {
        _suffix = suffix;
    }

    @Override
    public boolean matches(String str)
    {
        return str.endsWith(_suffix);
    }
}

public static StringMatcherImpl with(ElementMatcher<String> matcher) 
{
    return new StringMatcherImpl(matcher);
}

public static StringMatcherImpl startingWith(String prefix) 
{
    return with(new PrefixMatcher(prefix));
}

public static StringMatcherImpl endingWith(String prefix) 
{
    return with(new SuffixMatcher(prefix));
}

}

public class MatchElements
{

public static class ElementMatcherImpl<E> implements ElementMatcher<E> 
{
    private ElementMatcher<E> _matcher;

    ElementMatcherImpl(ElementMatcher<E> matcher) 
    {
        _matcher = matcher;
    }

    @Override
    public boolean matches(E e)
    {
        return _matcher.matches(e);
    }

    public ElementMatcherImpl<E> and(ElementMatcher<E> m)
    {
        _matcher = new ElementMatcherAndChain<E>(_matcher, m);
        return this;
    }

    public ElementMatcherImpl<E> or(ElementMatcher<E> m)
    {
        _matcher = new ElementMatcherOrChain<E>(_matcher, m);
        return this;
    }

    @Override
    public String toString()
    {
        return _matcher.toString();
    }
}

public static <E> ElementMatcherImpl<E> with(ElementMatcher<E> matcher) 
{
    return new ElementMatcherImpl<E>(matcher);
}

public static <E> ElementMatcherImpl<E> not(ElementMatcher<E> matcher)
{
    return with(new ElementMatcherNegator<E>(matcher));
}

}

1 个答案:

答案 0 :(得分:0)

public static StringMatcherImpl with(ElementMatcher<String> matcher)
<{1>}和

中的

MatchStrings

in public static <E> ElementMatcherImpl<E> with(ElementMatcher<E> matcher) type-erase to

MatchElements
从@Tom开始,

从Java 7开始不再有效。

如果可能,请将with(ElementMatcher matcher) 重命名为更具体的内容,例如with(ElementMatcher<String> matcher)