sách gpt4 ăn đã đi

java - MảngDanh sách danh sách = ArrayList mớiSự khác biệt giữa (); và Bộ sưu tập list1 = ArrayList mới();

In lại 作者:搜寻专家 更新时间:2023-11-01 04:02:04 29 4
mua khóa gpt4 giày nike

我不明白:

  1. ArrayList list = new ArrayList();
  2. Collection list1 = new ArrayList();

loại MảngDanh sách扩展实现接口(interface)的类 Bộ sưu tập , 所以类 MảngDanh sáchdụng cụ Bộ sưu tập界面。也许danh sách1允许我们使用 Bộ sưu tập 中的静态方法界面?

câu trả lời hay nhất

接口(interface)没有静态方法 [在 Java 7 中]。 danh sách1 只允许访问 Bộ sưu tập 中的方法,而 danh sách 允许访问 MảngDanh sách 中的所有方法。


最好用尽可能不具体的类型来声明一个变量。因此,例如,如果出于任何原因将 MảngDanh sách 更改为 Danh sách liên kết hoặc HashSet,则不必重构大部分代码(例如,客户端类)。

假设您有这样的东西(仅用于说明目的,不可编译):

class CustomerProvider {
public LinkedList getAllCustomersInCity(City city) {
// retrieve and return all customers for that city
}
}

然后您决定实现它并返回一个 HashSet。也许有一些客户端类依赖于您返回 Danh sách liên kết và gọi HashSet 没有的方法(例如 LinkedList.getFirst() )。

这就是为什么你最好这样做:

class CustomerProvider {
public Collection getAllCustomersInCity(City city) {
// retrieve and return all customers for that city
}
}

关于java - ArrayList list = new ArrayList(); 之间的区别和 Collection list1 = new ArrayList();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14163291/

29 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress