扩展和实现接口

时间:2015-01-21 02:21:49

标签: java c# interface

interface Processable<E>
{
    public E first();
    public E last();
    public List<E> processables();
    public E get(int index);
}

所以,我试图做的就是这个 以下是查询应该执行的示例

final WidgetItem item = Inventory.select().identify("Bones").note().first();

class Query: ObjectQuery, Processable<E>
{
    public E  
}

}

但是&#34; E&#34;

有错误

我尝试做的事情,但在java

public abstract class Query<E, Q extends Query<E, Q>> implements Processable<E> {

private final List<Filter<E>> filters;

public Query() {
    this.filters = new LinkedList<>();
}

@SafeVarargs
public final Q filter(final Filter<E>... filters) {
    Collections.addAll(this.filters, filters);
    return (Q) this;
}

protected final Filter<E> notThat(final Filter<E> filter) {
    return new Filter<E>() {
        @Override
        public boolean accepts(final E e) {
            return !filter.accepts(e);
        }
    };
}

/**
 * @param e
 *            the element
 * @return true if all of the filters accepted the element
 */
public final boolean accepts(final E e) {
    for (final Filter<E> filter : filters) {
        if (!filter.accepts(e))
            return false;
    }
    return true;
}

/**
 * @return a List of elements after filtering them all
 */
public final List<E> all() {
    final List<E> processables = processables();
    final ListIterator<E> iterator = processables.listIterator();
    while (iterator.hasNext()) {
        if (!accepts(iterator.next()))
            iterator.remove();
    }
    return processables;
}

public final E first() {
    final List<E> all = processables();
    return all.size() > 0 ? all.get(0) : null;
}

public final E last() {
    final List<E> all = processables();
    final int idx = all.size() - 1;
    return idx >= 0 ? all.get(idx) : null;
}

public final E get(final int index) {
    final List<E> all = processables();
    return all.size() > index ? all.get(index) : null;
}

}

此外,我收到List all = IProcessable的错误;这是List

2 个答案:

答案 0 :(得分:4)

class Query<E> : ObjectQuery, Processable<E>
{
    public E  
}

答案 1 :(得分:0)

您的界面声明需要从中删除公共可访问性内容,并将Get方法更改为索引器。我还将一些方法更改为Properties,并将Processables更改为IEnumerable Get属性:

public class Query<E>: ObjectQuery, IProcessable<E>
{
    public E this[int index]
    {
        get { throw new NotImplementedException(); }
    }

    public E First
    {
        get { throw new NotImplementedException(); }
    }

    public E Last
    {
        get { throw new NotImplementedException(); }
    }

    IEnumerable<E> IProcessable<E>.Processables
    {
        get { throw new NotImplementedException(); }
    }
}

interface IProcessable<E>
{
    E this[int index] { get; }
    E First { get; }
    E Last { get; }
    IEnumerable<E> Processables { get; }
}