简体   繁体   中英

Can I overload an interface in Java?

I want to define an interface with same name as other one, but different parameters. How can I do it? Please help.

public interface IFactory<T> {
    IFactory<T> Select(List<String> fields);
    IFactory<T> GroupBy(Expression<?> fields);
    IFactory<T> Where(Object column, ConditionalMethods conditionalMethod, Object... value);
    IFactory<T> And(Object column, ConditionalMethods conditionalMethod, Object... value);
    IFactory<T> Or(Object column, ConditionalMethods conditionalMethod, Object... value);

    T Take();
    T TakeNewObject();
    T TakeAndLock();
}

public interface IFactory<T, Z> {
    IFactory<T, Z> Select(List<String> fields);
    IFactory<T, Z> GroupBy(Expression<?> fields);
    IFactory<T, Z> Where(Object column, ConditionalMethods conditionalMethod, Object... value);
    IFactory<T, Z> And(Object column, ConditionalMethods conditionalMethod, Object... value);
    IFactory<T, Z> Or(Object column, ConditionalMethods conditionalMethod, Object... value);

    T Take();
    T TakeNewObject();
    T TakeAndLock();
}

I get following error:

java duplicate class: com.xxx.IFactory

You can't. You must use different names. That's how it is done in JDK interfaces.

For example, consider java.util.function.Function<T, R> vs. java.util.function.BiFunction<T, U, R> . Both are functional interfaces that represent a function, but the first represents a function with one argument (and a result) and the second represents a function with two arguments (and a result).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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