符号后的确切含义是什么?
List<Integer> list = new List<Integer>(){...}
Run Code Online (Sandbox Code Playgroud)
我可以说我创建了一个接口实例List<E>吗?而不是,因为我们无法在Java中创建任何接口的新实例.
下面是所有必须用这种表示法覆盖的方法.
但我不清楚究竟必须覆盖哪些方法.它们不是List<E>列表的所有方法或列表的超级接口 -Collection<E>, Iterable<E>
(没有eg hashCode()方法(如果它们都是List的方法)或者例如parallelStream()方法(如果它们都是List的Superinterfaces的继承方法))
List<Integer> list = new List<Integer>(){
public boolean add(Integer e) {...}
public void add(int index, Integer element) {...}
public boolean addAll(Collection<? extends Integer> c) {...}
public boolean addAll(int index, Collection<? extends Integer> c {...}
public void clear() {...}
public boolean contains(Object o) {...}
public boolean containsAll(Collection<?> c) {...}
public Integer get(int index) {...}
public int indexOf(Object o) {...}
public boolean isEmpty() {...}
public Iterator<Integer> iterator() {...}
public int lastIndexOf(Object o) {...}
public ListIterator<Integer> listIterator() {...}
public ListIterator<Integer> listIterator(int index) {...}
public boolean remove(Object o) {}
public Integer remove(int index) {...}
public boolean removeAll(Collection<?> c) {...}
public boolean retainAll(Collection<?> c) {...}
public Integer set(int index, Integer element) {...}
public int size() {...}
public List<Integer> subList(int fromIndex, int toIndex) {...}
public Object[] toArray() {...}
public <T> T[] toArray(T[] a) {...}
};
Run Code Online (Sandbox Code Playgroud)
new List<Integer>() { ... }是一个注释,用于创建实现List<Integer>接口的匿名类的实例.
您必须实现的方法是在List接口或其祖先接口中没有实现的所有方法.类的所有方法Object(例如hashCode())在所有接口中都有隐式实现.如果您使用Java 8,其他方法可能有默认实现(我假设是这样parallelStream()).
| 归档时间: |
|
| 查看次数: |
278 次 |
| 最近记录: |