gpt4 book ai didi

java - "new List()"符号是什么意思?

转载 作者:行者123 更新时间:2023-12-01 07:54:16 25 4
gpt4 key购买 nike

以下符号的确切含义是什么?

List<Integer> list = new List<Integer>(){...}

我可以说我创建了一个接口(interface) List<E> 的实例吗? ?相反,因为我们无法在 Java 中创建任何接口(interface)的新实例。

下面是必须用此表示法覆盖的所有方法。

但我不明白到底哪些方法必须被覆盖。它们也不全是 List<E> 中的方法。或列表的 super 接口(interface) - Collection<E>, Iterable<E>

(没有例如hashCode()方法(如果它们都是List的方法)或例如parallelStream()方法(如果它们都是从List的 super 接口(interface)继承的方法))

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) {...}

};

最佳答案

new List<Integer>() { ... }是一个注释,用于创建实现 List<Integer> 的匿名类的实例。界面。

您必须实现的方法是List中没有实现的所有方法。接口(interface)或其祖先接口(interface)。 Object的所有方法类(例如 hashCode() )在所有接口(interface)中都有隐式实现。如果您使用 Java 8,其他方法可能有默认实现(我假设 parallelStream() 就是这种情况)。

关于java - "new List()"符号是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32256286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com