简体   繁体   English

如何解决这种类型的擦除?

[英]How to solve this Type Erasure?

I'm wondering how can I solve this Compilation Failure I got in JDK 1.7, the code works perfectly with JDK 1.6. 我想知道如何解决在JDK 1.7中遇到的编译失败,该代码与JDK 1.6完美配合。

Error I got: MatchStrings.java:[71,36] error: name clash: with(ElementMatcher) in MatchStrings and with(ElementMatcher) in MatchElements have the same erasure, yet neither hides the other 我得到的错误: MatchStrings.java :[ 71,36]错误:名称冲突:MatchStrings中的with(ElementMatcher)和MatchElements中的with(ElementMatcher)具有相同的擦除,但是都没有隐藏另一个

My classes in questions: 我上课的问题:

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)

in MatchStrings and MatchStrings

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

in MatchElements type-erase to MatchElements类型擦除为

with(ElementMatcher matcher)

which is no longer valid, starting with Java 7, as @Tom said. 正如@Tom所说,它从Java 7开始不再有效。

If possible, rename with(ElementMatcher<String> matcher) to something more specific, like withStringMatcher(ElementMatcher<String> matcher) 如果可能,将with(ElementMatcher<String> matcher)重命名为更具体的名称,例如withStringMatcher(ElementMatcher<String> matcher)

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

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