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

我得到的错误:MatchStrings.java:[71,36]错误:名称冲突:MatchStrings中的with(ElementMatcher)和MatchElements中的with(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));
}

}

最佳答案

public static StringMatcherImpl with(ElementMatcher<String> matcher)


MatchStrings

public static <E> ElementMatcherImpl<E> with(ElementMatcher<E> matcher)


MatchElements类型中擦除

with(ElementMatcher matcher)


正如@Tom所说,它从Java 7开始不再有效。

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

09-10 20:54