8000 第二十章泛型通配符:将Fruit改为flist by nengquqiaoxiaoyun · Pull Request #632 · lingcoder/OnJava8 · GitHub
[go: up one dir, main page]

Skip to content

第二十章泛型通配符:将Fruit改为flist #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
将Fruit改为flist
原文:but to upcast to flist, that type is a “don’t actually care
  • Loading branch information
nengquqiaoxiaoyun committed Dec 14, 2020
commit 6f5a82a29883a7684f781345d9ed43438fd07b82
2 changes: 1 addition & 1 deletion docs/book/20-Generics.md
5A1E
Original file line number Diff line number Diff line change
Expand Up @@ -2666,7 +2666,7 @@ public class GenericsAndCovariance {
}
```

**flist** 的类型现在是 `List<? extends Fruit>`,你可以读作“一个具有任何从 **Fruit** 继承的类型的列表”。然而,这实际上并不意味着这个 **List** 将持有任何类型的 **Fruit**。通配符引用的是明确的类型,因此它意味着“某种 **flist** 引用没有指定的具体类型”。因此这个被赋值的 **List** 必须持有诸如 **Fruit** 或 **Apple** 这样的指定类型,但是为了向上转型为 **Fruit**,这个类型是什么没人在意。
**flist** 的类型现在是 `List<? extends Fruit>`,你可以读作“一个具有任何从 **Fruit** 继承的类型的列表”。然而,这实际上并不意味着这个 **List** 将持有任何类型的 **Fruit**。通配符引用的是明确的类型,因此它意味着“某种 **flist** 引用没有指定的具体类型”。因此这个被赋值的 **List** 必须持有诸如 **Fruit** 或 **Apple** 这样的指定类型,但是为了向上转型为 **flist**,这个类型是什么没人在意。

**List** 必须持有一种具体的 **Fruit** 或 **Fruit** 的子类型,但是如果你不关心具体的类型是什么,那么你能对这样的 **List** 做什么呢?如果不知道 **List** 中持有的对象是什么类型,你怎能保证安全地向其中添加对象呢?就像在 **CovariantArrays.java** 中向上转型一样,你不能,除非编译器而不是运行时系统可以阻止这种操作的发生。你很快就会发现这个问题。

Expand Down
0